Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Retrieving single value from an QXmlQuery
QtWS25 Last Chance

Retrieving single value from an QXmlQuery

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 3.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hsfougaris
    wrote on last edited by
    #1

    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.

    If you can't say what you mean, you'll never be able to mean what you say.

    1 Reply Last reply
    0
    • H Offline
      H Offline
      hsfougaris
      wrote on last edited by
      #2

      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?

      If you can't say what you mean, you'll never be able to mean what you say.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tobias.hunger
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • H Offline
          H Offline
          hsfougaris
          wrote on last edited by
          #4

          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.

          If you can't say what you mean, you'll never be able to mean what you say.

          1 Reply Last reply
          0
          • H Offline
            H Offline
            hsfougaris
            wrote on last edited by
            #5

            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();
            ...
            @

            If you can't say what you mean, you'll never be able to mean what you say.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved