getChildNodes 给出意外结果
我的 XML 看起来像这样 -
<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>
我正在使用下面的代码处理它
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 kids are”分别是 5 和 3?我不应该分别期待 2 和 1 吗? 因为第一个对象有“variable_value
”和“reference
”,而第二个对象只有“reference
”
本质上,我的目的是处理“的子对象”对象”。
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());
}
}
it outputs as-
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
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
"
Essentially, my intent is to process children of "objects".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确保
测试是否
应该足够。
Make sure you don't have whitespaces between <object> node children. Whitespaces are considered childnodes and returned as such.
Testing if
should be enough.
这是因为每个子节点之间有 2 个
TEXT_NODE
(#text
)。以下包括文本节点及其相应的值。
这可以通过修改代码来验证:
输出:
其中,3 =
TEXT_NODE
和 1 =ELEMENT_NODE
。That's because you have 2
TEXT_NODE
(#text
) between each child nodes.The following included the text nodes and their corresponding values.
This can be verified by modifying your code:
The output:
Where, 3 =
TEXT_NODE
and 1 =ELEMENT_NODE
.您只计算 ELEMENT 节点类型。如果您只对子元素感兴趣,您可以更改代码以包含以下检查
You are only counting ELEMENT node types. You can change your code to include the below check if you are interested in only child elements