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;
    device.setData(myQString.toUtf8());
    device.open(QIODevice::ReadOnly);

    QXmlQuery query;
    query.setQuery("doc($inputDocument)/query[myValueOfInterest]");
    query.bindVariable("inputDocument", &device);

    QXmlSerializer serializer(query, myOutputDevice);
    query.evaluateTo(&serializer);
    @

    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.

    thanks.



  • 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;
    device.setData(resp.toUtf8());
    device.open(QIODevice::ReadOnly);
    QXmlQuery query;

    query.setQuery("doc($inputDocument)/query[XMLActionResponse]");
    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="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
    <SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <NS1:XMLActionResponse xmlns:NS1="urn:SoapSrvURN">
    <return xsi:type="xsd:string>Some return value</return>
    </NS1:XMLActionResponse>
    </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
    @

    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;
    domDoc.setContent(resp2);
    QDomElement n = domDoc.firstChildElement();
    QDomNode _n = n.firstChild();
    qDebug() << _n.nodeValue();
    qDebug() << _n.firstChildElement().nodeName();
    ...
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.