Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QDomElement : Problem with text()?

QDomElement : Problem with text()?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    ShrikantAmbade
    wrote on last edited by ShrikantAmbade
    #1

    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

    1 Reply Last reply
    0
    • jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply
      0
      • jsulmJ jsulm

        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.

        S Offline
        S Offline
        ShrikantAmbade
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Try (domElement should be <name>):

          domElement.firstChild().text();
          

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved