简体   繁体   English

DOM解析器错误的childNodes计数

[英]DOM Parser wrong childNodes Count

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,我正在通过桌面的正常方式阅读并通过DOM解析器解析它。

<?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. 读取XML以获取孩子。

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. 所以这里我的逻辑运行良好。当我尝试通过将相同的XML推送到队列并读取它并获取子节点时,它给了我没有。 of child nodes is 6. 子节点数为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? 这搞砸了我解析XML的逻辑。任何人都可以帮助我吗?

UPDATE 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 : 问题如果我从桌面读取这个xml它会说13个孩子,6个元素节点和7个文本节点。通用逻辑是:

  • 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. 如果节点ISNOT文本节点进入if块,则添加一个带有两个子元素的父元素并附加到现有ROOT。然后获取NodeName并在元素节点之间获取TextContext并分别将它们作为setTextContext推送给两个子节点。
  • 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. 所以我现在有一个新的ELEMENT NODE,它有两个孩子。因为我现在不需要现有的元素节点,它仍然是root的子节点,最后正在删除它们。

So the above logic is all screwed if i am pushing the XML to queue and areading it for doing the same logic. 因此,如果我将XML推入队列并将其用于执行相同的逻辑,那么上述逻辑就完全搞砸了。

OUTPUT XML which is coming good when i read from desktop,but reading from queue is having problem, because it screw the complete tree. OUTPUT XML ,当我从桌面读取时,它正在变好,但是从队列中读取有问题,因为它拧紧了整个树。

<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. 如果包含空白文本节点,则有13个子节点,但如果删除了空白文本节点,则只有6个子节点。 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. 因此,在两种情况之间构建树的方式存在一些差异,这会影响是否保留空白文本节点。

The document under "Output XML" means that there is something wrong on the sender side. “输出XML”下的文档表示发送方存在问题。 My guess would by that inp isn't a String but some kind of object and setText(inp) doesn't call inp.toString() but instead triggers some kind of serialization code which produces this odd XML that you're seeing. 我的猜测是, inp不是一个String但某种对象和setText(inp) inp.toString() setText(inp)不会调用inp.toString() ,而是会触发某种序列化代码,从而产生你所看到的这种奇怪的XML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM