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 = 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??
-
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??
-
@JohnSRV
nodeValue()
only returns anything ifQDomNode
isQDomText
. IIRC, tryIpNode->firstChild()->nodeValue()
orIpNode->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 ??
EDIT
I also triedQDomText Test = IpNode.toText(); QString Type = Test.nodeValue(); printf(Type.toStdString().c_str());
Didn't work either.
-
@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 triedQDomText Test = IpNode.toText(); QString Type = Test.nodeValue(); printf(Type.toStdString().c_str());
Didn't work either.
-
@JohnSRV
Which doesn't sound right, but does match the behaviour. I don't know, sounds fishy. You should try this on all yourIpNode
s. You should make 100% sure you do not have otherIpNode
s 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. -
@JohnSRV
Which doesn't sound right, but does match the behaviour. I don't know, sounds fishy. You should try this on all yourIpNode
s. You should make 100% sure you do not have otherIpNode
s 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.