XML: accessing deep nodes
Hello. Is there a way to access XML nodes with something like this:
Code:
domDoc.elementsByTagName("methodResponse/params/param/value/struct/member")
instead of
Code:
QDomElement elem
= domDoc.
firstChildElement("methodResponse").
firstChildElement("params").
firstChildElement("param").
firstChildElement("value").
firstChildElement("struct").
firstChildElement("member")
Here follows the sample XML document. I need to get access to all 'member' nodes.
Code:
<?xml version="1.0" encoding="utf-8"?>
<methodResponse>
<params>
<param>
<value>
<struct>
<member>
<name>token</name>
<value>
<string>532236bdf362493h5</string>
</value>
</member>
<member>
<name>status</name>
<value>
<string>200 OK</string>
</value>
</member>
<member>
<name>seconds</name>
<value>
<double>0.008</double>
</value>
</member>
</struct>
</value>
</param>
</params>
</methodResponse>
Re: XML: accessing deep nodes
Re: XML: accessing deep nodes
Or You can use XPath to do the Query
Re: XML: accessing deep nodes
Ok, thanks. Some another question... I receive xml from the network. It seems like QXmlQuery allows to use QNetworkReply in bindVariable method, e.g.
Code:
query.bindVariable("MyXml", reply);
But it doesn't work: evaluateTo hangs up for 5-10 seconds, then I see "First-chance exception at 0x760eb727 in SnelNL.exe: Microsoft C++ exception: bool at memory location 0x0034c20f.." in the output window.
However if I read data to QByteArray and create QBuffer with this data, query works fine.
Is it possible to use QNetworkReply directly in QXmlQuery::bindVariable as XML source?
Re: XML: accessing deep nodes
Why not just use the doc() directive in the query itself?
Code:
QXmlQuery query;
query.setQuery("doc(http://www.qtcentre.org/index.php)/html/body/whatever");
Re: XML: accessing deep nodes
I need to make a post request in order to receive a reply from server
Re: XML: accessing deep nodes
In that case use QXmlQuery::setFocus() and pass it the network reply you receive. Just make sure you do it only after the data you expect has arrived.
Re: XML: accessing deep nodes
Thanks, but still the same problem when I pass a reply as QIODevice. Below is the slot that is connected to QNetworkAccessManager::finished signal, so all the data must be arrived, I think.
Code:
void MyClass::authenticationFinished(QNetworkReply *reply)
{
QXmlQuery query;
query.setFocus(reply);
query.setQuery("methodResponse/params/param/value/struct/member[name=\"token\"]/value/string(string)");
// ...
}
The following code works fine:
Code:
void MyClass::authenticationFinished(QNetworkReply *reply)
{
QXmlQuery query;
query.setFocus(reply->readAll()); // !pass the string
query.setQuery("methodResponse/params/param/value/struct/member[name=\"token\"]/value/string(string)");
// ...
}