getChildNodes给出了意想不到的结果

时间:2011-12-19 08:25:48

标签: java xml dom xml-parsing sax

我的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());

        }

}

输出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

我的问题是为什么&#34;#chidren是&#34;分别是5和3?我不应该分别期待2和1吗? 因为第一个对象有&#34; variable_value&#34;和&#34; reference&#34;第二个对象只有&#34; reference&#34;

基本上,我的目的是处理&#34;对象&#34;。

的孩子

3 个答案:

答案 0 :(得分:16)

确保&lt; object&gt;之间没有空格节点儿童。空格被视为子节点并以此形式返回。

测试是否

childNode.getNodeType() == Node.ELEMENT_NODE

应该足够了。

答案 1 :(得分:8)

那是因为每个子节点之间有2个TEXT_NODE#text)。

以下内容包括文本节点及其对应的值。

<object flag="complete" id="objId" version="1">
    <TEXT_NODE />
    <variable_value variable_id="varId">ValueGoesHere</variable_value>
    <reference item_ref="2"/>
    <TEXT_NODE />
</object>

可以通过修改代码来验证:

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

答案 2 :(得分:2)

您只计算ELEMENT节点类型。如果您只对子元素感兴趣,可以更改代码以包含以下检查

 if (aNode.getNodeType() == Node.ELEMENT_NODE) 
{
...
}