Important: Please read the Qt Code of Conduct -

Retrieving single value from an QXmlQuery

  • If I have a QString with an XML document, is the best way to retrieve a single node the following?

    QBuffer device;

    QXmlQuery query;
    query.bindVariable("inputDocument", &device);

    QXmlSerializer serializer(query, myOutputDevice);

    or is there some other API more appropriate?
    I'm asking because it seems very complicated for retrieving a single value (it's actually the response from a SOAP service), so I'd like to be sure.


  • Well, there must be something wrong with the above (or I am doing it wrong) because there is no joy...
    Here is my exact code:
    QBuffer device;
    QXmlQuery query;

    query.bindVariable("inputDocument", &device);
    QString * outStr = new QString();
    if (query.evaluateTo(outStr)) {
        qDebug() << "OK!"
    } else {
        qDebug() << "Error!";


    The XML (stored in the QString resp) is this:
    <?xml version="1.0"?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="" xmlns:xsd="" xmlns:xsi="" xmlns:SOAP-ENC="">
    <SOAP-ENV:Body SOAP-ENV:encodingStyle="">
    <NS1:XMLActionResponse xmlns:NS1="urn:SoapSrvURN">
    <return xsi:type="xsd:string>Some return value</return>

    I would expect to get "Some return value", or more likely "<return xsi:type="xsd:string>Some return value</return>", but instead evaluate returns false.

    What am I doing wrong?

  • Moderators

    Did you read the documentation of evaluateTo? That quite clearly states that the method does return a bool...

  • ok, you're right. I mean of course that I would expect the evaluateTo to return true, and I would then have the said strings in the outStr variable.

    So, it seems my query is wrong, but I haven't been able to get it right for my particular XML.

  • My workaround since the query is such a mystery:

    Isolate the response of interest in a string with a string operation, and operate on the actual response data with a QDomDocument.

    //resp holds our response from the SOAP service
    QString t1 = "<NS1:XMLActionResponse xmlns:NS1="urn:SoapSrvURN">";
    QString t2 = "</NS1:XMLActionResponse>";
    int pos1 = resp.indexOf(t1);
    if (pos1 == -1) return;
    pos1 += t1.length();
    int pos2 = resp.indexOf(t2);
    if (pos2 == -1) return;
    QString resp2 = resp.mid( pos1, pos2 -pos1 );
    //resp2 now has only the 'answer' from the SOAP service

    QDomDocument domDoc;
    QDomElement n = domDoc.firstChildElement();
    QDomNode _n = n.firstChild();
    qDebug() << _n.nodeValue();
    qDebug() << _n.firstChildElement().nodeName();

Log in to reply