Nominate our 2022 Qt Champions!

QDomElement : Problem with text()?

  • Hello Everyone,
    I am trying to read text of QDomElement. But the problem is that QDomelement has another two nodes and that nodes also has when I read my QDomElement element its child text also appear in it but I only want its text not its child text..

    For Example:

    <name> U
    void ImportXML::Name(QDomElement  childN) {
        childNEle = childN;
      qDebug() << "Text of Name node is: " <<ChildName;
      if (! NName.contains(childN.text())&& childN.nodeName()!="pos"&& childN.nodeName()!="size"){
           qDebug() << "Text of Name node is: " <<ChildName;

    now when I read QDomelement name text then it gives me value like this "U0,193162,162" but I only want "U"

    What to do then??
    Many Thanks in advance

  • Lifetime Qt Champion

    Check this:

    Text is Always Stored in Text Nodes
    A common error in DOM processing is to expect an element node to contain text.
    However, the text of an element node is stored in a text node.
    In this example: <year>2005</year>, the element node <year>, holds a text node with the value "2005".
    "2005" is not the value of the <year> element!

    It is not a bug.
    And your XML is a bit unusual (but valid): <name> contains a text node (U) and other nodes.

  • @jsulm Thanks ... but my problem is How to ignore the text of other nodes

  • Lifetime Qt Champion

    Try (domElement should be <name>):


Log in to reply