简体   繁体   English

getChildNodes 给出意外结果

[英]getChildNodes giving unexpected result

My XML looks like this-我的 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>

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?我的问题是为什么“孩子的数量”分别是 5 和 3? Shouldn't I be expecting 2 and 1 respectively ?我不应该分别期待 2 和 1 吗? because first object has " variable_value " and " reference " and second object has only " reference "因为第一个对象有“ variable_value ”和“ reference ”,第二个对象只有“ reference

Essentially, my intent is to process children of "objects".从本质上讲,我的目的是处理“对象”的孩子。

Make sure you don't have whitespaces between <object> node children.确保 <object> 节点子节点之间没有空格。 Whitespaces are considered childnodes and returned as such.空格被视为子节点并按原样返回。

Testing if测试是否

childNode.getNodeType() == Node.ELEMENT_NODE

should be enough.应该够了。

That's because you have 2 TEXT_NODE ( #text ) between each child nodes.那是因为每个子节点之间有 2 个TEXT_NODE ( #text )。

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>

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

            }

        }

The output:输出:

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

Where, 3 = TEXT_NODE and 1 = ELEMENT_NODE .其中, 3 = TEXT_NODE和 1 = ELEMENT_NODE

You are only counting ELEMENT node types.您只计算 ELEMENT 节点类型。 You can change your code to include the below check if you are interested in only child elements如果您只对子元素感兴趣,您可以更改代码以包含以下检查

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM