問題描述
我的 XML 看起來像這樣 -
My XML looks like this-
<collected_objects>
<object flag="complete" id="objId" version="1">
<variable_value variable_id="varId">ValueGoesHere</variable_value>
<reference item_ref="2"/>
</object>
<object comment="objComment" flag="complete" id="objId" version="1">
<reference item_ref="1"/>
</object>
</collected_objects>
我正在使用下面的代碼處理它-
I am processing it using below code-
Document dom = parser.getDocument();
NodeList collected_objects = dom.getElementsByTagName("object");
System.out.println("Number of collected objects are " + collected_objects.getLength());
for (int i = 0; i < collected_objects.getLength(); i++) {
Node aNode = collected_objects.item(i);
//get children of "objects"
NodeList refNodes = aNode.getChildNodes();
System.out.println("# of chidren are " + refNodes.getLength());
//print attributes of "objects"
NamedNodeMap attributes = aNode.getAttributes();
for (int a = 0; a < attributes.getLength(); a++) {
Node theAttribute = attributes.item(a);
System.out.println(theAttribute.getNodeName() + "=" + theAttribute.getNodeValue());
}
}
它輸出為-
Number of collected objects are 2
# of chidren are 5
flag=complete
id=objId
version=1
# of chidren are 3
comment=objComment
flag=complete
id=objId
version=1
我的問題是為什么# of chidren are"分別是 5 和 3?我不應(yīng)該分別期待 2 和 1 嗎?因?yàn)榈谝粋€對象有variable_value
"和reference
",而第二個對象只有reference
"
My question is why "# of chidren are" are 5 and 3 respectively? Shouldn't I be expecting 2 and 1 respectively ?
because first object has "variable_value
" and "reference
" and second object has only "reference
"
本質(zhì)上,我的意圖是處理對象"的子級.
Essentially, my intent is to process children of "objects".
推薦答案
那是因?yàn)槊總€子節(jié)點(diǎn)之間有2個TEXT_NODE
(#text
).
That's because you have 2 TEXT_NODE
(#text
) between each child nodes.
以下包括文本節(jié)點(diǎn)及其對應(yīng)的值.
The following included the text nodes and their corresponding values.
<object flag="complete" id="objId" version="1">
<TEXT_NODE />
<variable_value variable_id="varId">ValueGoesHere</variable_value>
<reference item_ref="2"/>
<TEXT_NODE />
</object>
這可以通過修改代碼來驗(yàn)證:
This can be verified by modifying your code:
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document dom = dBuilder.parse(new ByteArrayInputStream(S.getBytes()));
NodeList collected_objects = dom.getElementsByTagName("object");
System.out.println("Number of collected objects are "
+ collected_objects.getLength());
for (int i = 0; i < collected_objects.getLength(); i++) {
Node aNode = collected_objects.item(i);
// get children of "objects"
NodeList refNodes = aNode.getChildNodes();
System.out.println("# of chidren are " + refNodes.getLength());
//
for (int x = 0; x < refNodes.getLength(); x++) {
Node n = refNodes.item(x);
System.out.println(n.getNodeType() + " = " + n.getNodeName() + "/" + n.getNodeValue());
}
// print attributes of "objects"
NamedNodeMap attributes = aNode.getAttributes();
for (int a = 0; a < attributes.getLength(); a++) {
Node theAttribute = attributes.item(a);
System.out.println(theAttribute.getNodeName() + "="
+ theAttribute.getNodeValue());
}
}
輸出:
Number of collected objects are 2
# of chidren are 5
3 = #text/
1 = variable_value/null
3 = #text/
1 = reference/null
3 = #text/
flag=complete
id=objId
version=1
# of chidren are 3
3 = #text/
1 = reference/null
3 = #text/
comment=objComment
flag=complete
id=objId
version=1
其中,3 = TEXT_NODE
和 1 = ELEMENT_NODE
.
Where, 3 = TEXT_NODE
and 1 = ELEMENT_NODE
.
這篇關(guān)于getChildNodes 給出意外的結(jié)果的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!