QXmlQuery and XPath expression



  • Hello,

    How can I use QXmlQuery to execute an XPath expression?

    Here is some test code that compiles but does not work;

    @
    #include <QDomDocument>
    #include <QXmlQuery>
    #include <QFile>
    #include <QDebug>

    int main(int argc, char *argv[])
    {
    // Test code
    QFile file("Books.xml");
    file.open(QFile::ReadOnly);

    QXmlQuery query(QXmlQuery::XPath20);
    query.setFocus(&file);
    query.setQuery("//Book");
    bool validQuery = query.isValid();
    qDebug() << "QXmlQuery::isValid: " << validQuery;
    
    QStringList results;
    query.evaluateTo(&results);
    int count = results.count();
    qDebug() << "QStringList::Count: " << count;
    
    return 0;
    

    }
    @

    Books.xml looks like this;
    @
    <?xml version="1.0" encoding="utf-8"?>
    <Books>
    <Book>abc</Book>
    <Book>def</Book>
    <Book>ghi</Book>
    </Books>
    @

    According to QXmlQuery the query is valid but the result is always 0. Have tried various combinations of the query but nothing seems to work.

    The XPath expression works when testing it here: http://www.bit-101.com/xpath/

    Appreciate some help.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.