1

我尝试在 xml 中为每个产品获取“报价”。

结构看起来像这样

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

报价如下所示:

 <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"/>

问题是我可以将所有值检索到 QStringList 但我不能这样做来分隔变量,例如 @price_retail/string()

我将发布我的代码:

    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();
}
4

1 回答 1

2

由于我有同样的问题,我在寻找解决方案时发现了这篇文章。这对我有用(QT5 Archlinux+KDE)

只需添加QXmlNamePool一个QXmlQuery

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

似乎内部查询(从 QXmlItem 获取其 fokus)没有正确设置其名称,因此无法识别属性名称(在本例中为 @url)。

这可能与 Qt-Doc 中的一点有关,QXmlNamePool当您想稍后在程序中比较名称时,您应该保留这一点。

于 2015-02-10T17:51:46.297 回答