问题描述
这很奇怪,但让我尽力解决.
This is strange but let me try my best to put it accross.
我有一个 XML,我通过正常方式从桌面读取并通过 DOM 解析器对其进行解析.
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>
读取 XML 以获取子项.
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
所以在这里我的逻辑运行良好.当我试图通过将相同的 XML 推送到队列并读取它并获取子节点时它给我没有.子节点数为 6.
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
这搞砸了我解析 XML 的逻辑.谁能帮帮我?
this screws my logic of parsing the XML.Can anyone help me out?
更新
添加发送逻辑:
javax.jms.TextMessage tmsg = session.createTextMessage();
tmsg.setText(inp);
sender.send(tmsg);
问题如果我从桌面读取此 xml,它会显示 13 个子节点、6 个元素节点和 7 个文本节点.通用逻辑是:
PROBLEM If i read this xml from desktop it says 13 childs, 6 element node and 7 text nodes.The Common Logic is :
- 读取所有子项并遍历子项列表.
- 如果节点ISNOT文本节点进入if块,添加一个带有两个孩子的父元素并附加到现有ROOT.然后获取NodeName并获取元素节点之间的TextContext并将它们分别推送为两个孩子的setTextContext.
- 所以我现在有一个新的元素节点,它有两个子节点.由于我现在不需要已经存在的元素节点,它们仍然是 root 的子节点,所以最后要删除它们.
因此,如果我将 XML 推送到队列并对其进行分区以执行相同的逻辑,则上述逻辑全都搞砸了.
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,当我从桌面读取时会很好,但是从队列读取有问题,因为它会破坏完整的树.
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>
谢谢
推荐答案
好吧,如果包含空白文本节点,则有 13 个子节点,但如果删除空白文本节点,则只有 6 个子节点.因此,这两种情况下树的构建方式存在一些差异,这会影响是否保留空白文本节点.
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 Parser 错误 childNodes Count的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!