Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QXmlQuery and Xpath : cannot extract neither the node's text nor the parameter’s value
Forum Updated to NodeBB v4.3 + New Features

QXmlQuery and Xpath : cannot extract neither the node's text nor the parameter’s value

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 508 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hisoft
    wrote on 19 Feb 2015, 10:01 last edited by
    #1

    Hi,

    I'm trying to extract some data from an xml content. So i used QXmlQuery and wrote the code below (in Qt5). The problems are :

    1. If i try to extract text1 (so a node's text, see line 32), the code returns me the whole node : <value>text1</value>

    => Question 1 : how to get only text1 ?

    1. If i try to extract x1 (so a parameter's value, see line 35), the code returns me an error :
      Error SENR0001 in file:///C:/work/tests/build-TestXMLParser-Desktop-Debug/debug/TestXMLParser.exe, at line 1, column 1: Attribute param can't be serialized because it appears at the top level.

    => Question 2 : What i'm doing wrong ?

    Here is the code :
    @
    #include <QApplication>

    #include <QFile>
    #include <QDebug>
    #include <QXmlQuery>
    #include <QXmlSerializer>
    #include <QXmlFormatter>
    #include <QBuffer>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    QString xmlContent =
            "<nodes>"
                "<node1 param='x1'>"
                    "<value>text1</value>"
                "</node1>"
                "<node2 param='x2'>"
                    "<value>text2</value>"
                "</node2>"
            "</nodes>";
    
    QBuffer device;
    device.setData(QByteArray(xmlContent.toUtf8().constData()));
    device.open(QIODevice::ReadOnly);
    
    QXmlQuery query;
    query.bindVariable("inputDocument", &device);
    
    // Extracting "text1"
    query.setQuery(QString("doc($inputDocument)/nodes/node1/value[text()]"));
    
    // Exracting "x1"
    //query.setQuery(QString("doc($inputDocument)/nodes/node1/@param"));
    
    // Output value
    QByteArray outArray;
    QBuffer buffer(&outArray);
    buffer.open(QIODevice::ReadWrite);
    
    QXmlSerializer serializer(query, &buffer);
    query.evaluateTo(&serializer);
    
    buffer.close();
    qWarning() << "Exracted value : " << QString::fromUtf8(outArray.constData());
    
    exit(0);
    
    return a.exec(&#41;;
    

    }
    @

    Thanks

    1 Reply Last reply
    0

    1/1

    19 Feb 2015, 10:01

    • Login

    • Login or register to search.
    1 out of 1
    • First post
      1/1
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved