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 text..so 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
    <pos>0,193</pos>
    <size>162,162</size>
    </name>
    
    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"){
           ChildName=childN.text();
           qDebug() << "Text of Name node is: " <<ChildName;
            NName.append(childN.text());
      }
    
    }
    

    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


  • Moderators

    Check this: http://www.w3schools.com/xml/dom_nodes.asp

    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


  • Moderators

    Try (domElement should be <name>):

    domElement.firstChild().text();
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.