問題描述
這很奇怪,但讓我盡力解決.
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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!