user891908
user891908

Reputation: 261

query.evaluteTo works for QStringList but not for specific items from xml

I try to get a "Offer" for each product in xml.

The structure looks like this

<response>
 <results>
  <products>
   <product>
     <offers>
       <offer>
       <offer>//HERE IS A PROBLEM
   <product>
     <offers>
       <offer>
       <offer>

the offer looks like this:

 <offer price_retail="10.99" percent_off="23.02" merchant="101" currency_iso="USD" price_merchant="8.46" image_url_large="" description="Description " name="111 Musician's Gear T" id="2822961" url="http://specificlink.com"/>

Problem is that i can retrieve all values to QStringList but i can't do it to separate variables like @price_retail/string()

I'll post my code:

    QXmlQuery queryOffers;
    QXmlQuery query1;
    query1.bindVariable("mySearch", &searchXml);
    query1.setQuery("declare variable $mySearch external;doc($mySearch)/response/results/products/product");

     QXmlResultItems items;
     query1.evaluateTo(&items);
     QXmlItem item( items.next() );

     while( !item.isNull() )
         {
             query1.setFocus(item);

             QString prodDesc;
             query1.setQuery("@description/string()");
             query1.evaluateTo(&prodDesc);

             QXmlResultItems itemsOffers;
             query1.setQuery("offers/offer");
             query1.evaluateTo(&itemsOffers);

             QXmlItem offer( itemsOffers.next() );

             while(!offer.isNull()){

                 QString offerUrl;
                 QString offerList;

                 queryOffers.setFocus(offer);

                   queryOffers.setQuery("@*/string()");
                   queryOffers.evaluateTo(&offerList);
                   qDebug()<<offerList;  //This returns all values
                   queryOffers.setQuery("@url/string()");
                   queryOffers.evaluateTo(&offerUrl);
                   qDebug()<<offerUrl; //this returns empty

                 offer = itemsOffers.next();
             }




             item = items.next();
}

Upvotes: 1

Views: 181

Answers (1)

edisn
edisn

Reputation: 68

Since I had the same problem, I found this post on my search for a solution. Here is what worked for me (QT5 Archlinux+KDE)

Just add a QXmlNamePool to the QXmlQuery:

    QXmlNamePool  m_namePool;
    QXmlQuery queryOffers(m_namePool);
    QXmlQuery query1(m_namePool); 

It seems the inner query (which gets its fokus from a QXmlItem) doesn't set up its names correctly, and therefore doesn't recognise the attribute name (in this case @url).

It might have to do with the point in Qt-Doc that you should keep around the QXmlNamePool when you want to compare the names later in your program.

Upvotes: 2

Related Questions