我尝试在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();
}
答案 0 :(得分:2)
由于我遇到了同样的问题,我在寻找解决方案时发现了这篇文章。 这对我有用(QT5 Archlinux + KDE)
只需向QXmlNamePool
添加QXmlQuery
:
QXmlNamePool m_namePool;
QXmlQuery queryOffers(m_namePool);
QXmlQuery query1(m_namePool);
似乎是内部查询(从QXmlItem获取它的fokus) 没有正确设置其名称,因此无法识别属性名称(在本例中为@url)。
如果您希望稍后在程序中比较这些名称,则可能与Qt-Doc中的要点有关。{/ 1}}。