我尝试在 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();
}