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. Can't get the Value of a Node in XML

Can't get the Value of a Node in XML

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 807 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.
  • J Offline
    J Offline
    JohnSRV
    wrote on last edited by
    #1

    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??

    JonBJ 1 Reply Last reply
    0
    • J JohnSRV

      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??

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

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

      J 1 Reply Last reply
      0
      • JonBJ JonB

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

        J Offline
        J Offline
        JohnSRV
        wrote on last edited by JohnSRV
        #3

        @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.

        JonBJ 1 Reply Last reply
        0
        • J 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.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

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

          J 1 Reply Last reply
          0
          • JonBJ JonB

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

            J Offline
            J Offline
            JohnSRV
            wrote on last edited by
            #5

            @JonB
            returns 21 which is QDomNode::BaseNode

            JonBJ 1 Reply Last reply
            0
            • J JohnSRV

              @JonB
              returns 21 which is QDomNode::BaseNode

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @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
              0
              • JonBJ 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 Offline
                J Offline
                JohnSRV
                wrote on last edited by
                #7

                @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
                1

                • Login

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