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
Forum Updated to NodeBB v4.3 + New Features

Can't get the Value of a Node in XML

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 821 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 30 Jul 2020, 09:52 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??

    J 1 Reply Last reply 30 Jul 2020, 09:59
    0
    • J JohnSRV
      30 Jul 2020, 09:52

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

      J Offline
      J Offline
      JonB
      wrote on 30 Jul 2020, 09:59 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 30 Jul 2020, 10:09
      0
      • J JonB
        30 Jul 2020, 09:59

        @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 30 Jul 2020, 10:09 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.

        J 1 Reply Last reply 30 Jul 2020, 11:17
        0
        • J JohnSRV
          30 Jul 2020, 10:09

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

          J Offline
          J Offline
          JonB
          wrote on 30 Jul 2020, 11:17 last edited by
          #4

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

          J 1 Reply Last reply 30 Jul 2020, 12:05
          0
          • J JonB
            30 Jul 2020, 11:17

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

            J Offline
            J Offline
            JohnSRV
            wrote on 30 Jul 2020, 12:05 last edited by
            #5

            @JonB
            returns 21 which is QDomNode::BaseNode

            J 1 Reply Last reply 30 Jul 2020, 12:29
            0
            • J JohnSRV
              30 Jul 2020, 12:05

              @JonB
              returns 21 which is QDomNode::BaseNode

              J Offline
              J Offline
              JonB
              wrote on 30 Jul 2020, 12:29 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 30 Jul 2020, 13:31
              0
              • J JonB
                30 Jul 2020, 12:29

                @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 30 Jul 2020, 13:31 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

                2/7

                30 Jul 2020, 09:59

                5 unread
                • Login

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