Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved Can't get the Value of a Node in XML

    General and Desktop
    2
    7
    344
    Loading More Posts
    • 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.
    • J
      JohnSRV last edited by

      Hello Everyone,

      I'm having a weird situation where i can't get the value of a Node from an XML File. After I load the XML Data I search for a specific Tag and try to store its value in a QString. This will be later used for other purposes. Here's the code:

      	QDomNodeList TargetList = Routes.elementsByTagName("IpAddr");
      	qDebug() << TargetList.size();
      	for (int h = 0; h < TargetList.count(); h++)
      	{
      		QDomNode IpNode = TargetList.at(h);
      		QString CurrAttr = IpNode.nodeValue();
      		printf(CurrAttr.toStdString().c_str());
      	
      

      The TargetList.size() returns 1 which is correct since there's only one node with the name "IpAddr". IpNode.nodeName() returns also "IpAddr" which means the code finds the node. The Problem is that IpNode.nodeValue returns an empty string.

      Here's how it looks in the XML File:

      <IpAddr>192.168.10.10</IpAddr>
      

      shouldn't IpNode.nodeValue() return "192.168.10.10"? Why do I keep getting an empty String??

      JonB 1 Reply Last reply Reply Quote 0
      • JonB
        JonB @JohnSRV last edited by JonB

        @JohnSRV
        nodeValue() only returns anything if QDomNode is QDomText. IIRC, try IpNode->firstChild()->nodeValue() or IpNode->firstChildElement()->nodeValue()?

        J 1 Reply Last reply Reply Quote 0
        • J
          JohnSRV @JonB last edited by JohnSRV

          @JonB I already tried those two alternatives but they didn't do the trick. Is there a way to convert QDomNode to QDomText ??

          EDIT
          I also tried

          QDomText Test = IpNode.toText();
          QString Type = Test.nodeValue();
          printf(Type.toStdString().c_str());
          

          Didn't work either.

          JonB 1 Reply Last reply Reply Quote 0
          • JonB
            JonB @JohnSRV last edited by

            @JohnSRV
            qDebug() << IpNode->nodeType() ?

            J 1 Reply Last reply Reply Quote 0
            • J
              JohnSRV @JonB last edited by

              @JonB
              returns 21 which is QDomNode::BaseNode

              JonB 1 Reply Last reply Reply Quote 0
              • JonB
                JonB @JohnSRV last edited by JonB

                @JohnSRV
                Which doesn't sound right, but does match the behaviour. I don't know, sounds fishy. You should try this on all your IpNodes. You should make 100% sure you do not have other IpNodes in your document which you are not showing to us and are empty, etc. You should try your code on a small, very simple test document to try it out. That sort of thing.

                J 1 Reply Last reply Reply Quote 0
                • J
                  JohnSRV @JonB last edited by

                  @JonB I made my way around it by converting the QDomNode to QDomElement. I then used QDomElement::text. It returns the Ip Adress as QString. So problem solved.

                  Still I don't quite understand the difference between QDomElement and QDomNode.

                  1 Reply Last reply Reply Quote 1
                  • First post
                    Last post