qtqtxmlqdomdocument

QDomElement::text() without child element texts?


I have an xml like:

<a>
  <b>1</b>
  <c>2</c>
  <d>3</d>
</a>

and a recursive function that parses QDomDocument that wraps it. The function iterates QDomNodes, converting them into QDomElements and calls text() method to get data. Unfortunately QDomElement::text() works at <a> level too and returns: 123. So it gathers the texts of all nested elements.
I would like it to return an empty string bcs, I would rather not checking tagName() value as there are may be plenty of them. So I would rather chek node tag by haveng/not having text inside than vice versa. Is this doable? Is there a method that will return empty string for <a> and text values for <b>, <c>, <d> levels?
P.S. QDomNode::nodeValue() returns an empty text for all elements.


Solution

  • It seems I was wrong bcs I was not iterating QDomNodes that can't be converted to QDomElements. And according to this answer:

    This is required by the DOM specification:

    The Text interface represents the textual content (termed character data in XML) of an Element or Attr. If there is no markup
    inside an element's content, the text is contained in a single object
    implementing the Text interface that is the only child of the element.
    If there is markup, it is parsed into a list of elements and Text
    nodes that form the list of children of the element.
    

    I have no markup inside <b>-like elements. So at <b> element's level I'm having el.childNodes().size() == 1, el.firstChild().isElement() == false and el.firstChild().nodeValue() returns a non-empty value!