DOM解析器错误的子节点计数 [英] DOM Parser wrong childNodes Count

查看:90
本文介绍了DOM解析器错误的子节点计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这很奇怪,但让我尽力把它放在一起。



我有一个XML,我正在从桌面阅读,并通过解析DOM解析器。

 <?xml version =1.0encoding =UTF-8?> 
< Abase
xmlns =www.abc.com/Events/Abase.xsd>
< FVer> 0< / FVer>
< DV> abc App< / DV>
< DP> abc电子钱包< / DP>
< Dversion> 11< / Dversion>
< sigID> Ss22< / sigID>
< activity>添加新蛋糕< / activity>
< / Abase>

阅读XML以获取小孩。

 文档doc = docBuilder.parse(C://Users//Desktop//abc.xml); 
Node root = doc.getElementsByTagName(Abase)。item(0);
NodeList listOfNodes = root.getChildNodes(); // Sysout Prints 13

所以这里我的逻辑运行良好。当我试图通过推动相同XML到一个队列并读取它,并得到它给我的小孩节点没有。的子节点是6。

 文档doc = docBuilder.parse(new InputSource(new ByteArrayInputStream(msg.getBytes(UTF- 8\" )))); 
Node root = doc.getElementsByTagName(Abase)。item(0);
NodeList listOfNodes = root.getChildNodes(); // Sysout Prints 6

这是我解析XML的逻辑,任何人都可以帮助我吗? / p>

更新



添加发送逻辑:

  javax.jms.TextMessage tmsg = session.createTextMessage(); 
tmsg.setText(inp);
sender.send(tmsg);

PROBLEM
如果我从桌面读取这个xml, 13个孩子,6个元素节点和7个文本节点。常见逻辑是:




  • 读取所有的孩子并遍历子项目列表。

  • 如果节点ISNOT文本节点进入if块,则添加一个带有两个子节点的父元素并附加到现有的ROOT。然后获取NodeName并在元素节点之间获取TextContext并将其推送为setTextContext两个孩子分别。

  • 所以我现在有一个新的ELEMENT NODE现在有两个孩子,而且我现在不需要已经存在的元素节点,仍然是root的孩子,最后是删除它们。



所以上面的逻辑全部都是拧紧的,如果我正在推送XML进行排队,并将其作为相同的逻辑排列。



OUTPUT XML 当我从桌面阅读时,这是很好的,但是从队列中读取有问题,因为它会将comp lete tree。

 < Abase 
xmlns =www.abc.com/Events/Abase.xsd> ;
< Prop>
< propName> FVer< / propName>
< propName> 0< / propName> //对于其他节点,类似的是
< / Prop>
< / Abase>

谢谢

解决方案

如果包含空白文本节点,则有13个子节点,但如果空格文本节点被删除,则只有6个子节点。因此,在两种情况下构建树的方式有一些差异,影响是否保留空格文本节点。


This is strange but let me try my best to put it accross.

I have a XML which i am reading through the normal way from desktop and parsing it through DOM parser.

<?xml version="1.0" encoding="UTF-8"?>
<Abase
    xmlns="www.abc.com/Events/Abase.xsd">
    <FVer>0</FVer>
    <DV>abc App</DV>
    <DP>abc Wallet</DP>
    <Dversion>11</Dversion>
    <sigID>Ss22</sigID>
    <activity>Adding New cake</activity>
</Abase>

Reading the XML to get the childs.

Document doc = docBuilder.parse("C://Users//Desktop//abc.xml");
Node root = doc.getElementsByTagName("Abase").item(0);
NodeList listOfNodes = root.getChildNodes();            //Sysout Prints 13

So here my logic works well.When am trying to do by pushing the same XML to a queue and read it and get the child nodes it gives me no. of child nodes is 6.

Document doc=docBuilder.parse(new InputSource(new ByteArrayInputStream(msg.getBytes("UTF-8"))));
Node root = doc.getElementsByTagName("Abase").item(0);
NodeList listOfNodes = root.getChildNodes();            //Sysout Prints 6

this screws my logic of parsing the XML.Can anyone help me out?

UPDATE

Adding sending logic :

javax.jms.TextMessage tmsg = session.createTextMessage();
tmsg.setText(inp);
sender.send(tmsg);

PROBLEM If i read this xml from desktop it says 13 childs, 6 element node and 7 text nodes.The Common Logic is :

  • Read all the childs and iterate through list of child items.
  • If node ISNOT text node get inside if block,add one parent element with two child and append to existing ROOT.Then get NodeName and get TextContext between the element node and push them as setTextContext for both the childs respectively.
  • So i have a fresh ELEMENT NODE now which have two childs .And as i dont need the already existing element node now which are still the childs of root,Lastly am removing them.

So the above logic is all screwed if i am pushing the XML to queue and areading it for doing the same logic.

OUTPUT XML which is coming good when i read from desktop,but reading from queue is having problem, because it screw the complete tree.

<Abase
    xmlns="www.abc.com/Events/Abase.xsd">
<Prop>
<propName>FVer</propName>
<propName>0</propName> //similarly for other nodes
</Prop>
</Abase>

Thanks

解决方案

Well, there are 13 children if whitespace text nodes are included, but only 6 if whitespace text nodes are dropped. So there's some difference in the way the tree has been built between the two cases, that affects whether whitespace text nodes are retained or not.

这篇关于DOM解析器错误的子节点计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆