Search code examples
c++xerces

xerces c++ getChildNodes getLength


I am trying to parse am XML string using xerces c++.

The structure is

<root>
<optionA>
<optionB/>
</optionA>
</root>

I read the xml string into MemBufInputSource and then parse it.

When I call getChildNodes() on root, it always returns 2. Should it not be 1? Here, only option A is the child of root. Also, for each child I check if its a node and of type element. For the first child, the check is always false.

Why does it show a count of 2 children?


Solution

  • getChildNodes() returns all child nodes, not just the ones that are elements.

    The whitespace between the elements (new lines in this case) count as a text node (DOMNode::NodeType::TEXT_NODE). By my count there are actually 2 text nodes in your example, so 3 child nodes overall, though differences when transcribing into the question, or different configuration of Xerces may have resulted in 2 child nodes in your original code.

    If you change your XML example to be all on one line with no whitespace

    <root><optionA><optionB/></optionA></root>

    you can see that Xerces will then report that there is only one child of root.

    Here is the full list of node types that Xerces may encounter.