Important: Please read the Qt Code of Conduct -

Can't get the Value of a Node in XML

  • 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 =;
    		QString CurrAttr = IpNode.nodeValue();

    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:


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

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

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

    I also tried

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

    Didn't work either.

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

  • @JonB
    returns 21 which is QDomNode::BaseNode

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

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

Log in to reply