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. [Solved] QXmlQuery returns null item
Forum Updated to NodeBB v4.3 + New Features

[Solved] QXmlQuery returns null item

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 1.0k 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.
  • J Offline
    J Offline
    johnlubest
    wrote on last edited by
    #1

    Hi, I'm using a code probed to work with others xquery files, but in this case my code is not working.

    Here is xml file i'm using:
    @<comandos>
    <comando servicio="plafones" on=true id="1">2cp0021\n\r</comando>
    <comando servicio="plafones" on=true id="2">2cp0023\n\r</comando>
    <comando servicio="plafones" on=true id="3">2cp0025\n\r</comando>
    <comando servicio="plafones" on=false id="1">2cp0022\n\r</comando>
    <comando servicio="plafones" on=false id="2">2cp0024\n\r</comando>
    <comando servicio="plafones" on=false id="3">2cp0026\n\r</comando>
    </comandos>@

    Note: true and false are between double quotes in real file, I had to delete in here because on parameter got erased in the post.

    Here is xquery file:
    @declare variable $inputDocument external;
    declare variable $servicio external;
    declare variable $on external;
    declare variable $id external;

    doc($inputDocument)//comandos/comando[@servicio=xs:string($servicio) and @on=xs:boolean($on) and @id=xs:integer($id)]/string()@

    xmlpatterns console command returns the correct string as result:
    @$ xmlpatterns -param inputDocument=comando.xml -param servicio=plafones -param on=true -param id=1 comando.xq
    2cp0021\n\r@

    And here is the piece of code that uses all this stuff:
    @QString ConfigSistema::getComando(QString servicio, bool on, int id)
    {
    QString comando = "null";
    QFile instanceFile(getXMLPrincipal());
    instanceFile.open(QIODevice::ReadOnly);

    QFile xq(":/xquery/xml/xquery/comando.xq");
    xq.open(QIODevice::ReadOnly);

    QXmlQuery query;
    query.bindVariable("inputDocument", &instanceFile);
    query.bindVariable("servicio", QXmlItem(QVariant(servicio)));
    query.bindVariable("on", QXmlItem(QVariant(on)));
    query.bindVariable("id", QXmlItem(QVariant(id)));

    if(xq.isOpen()){
    query.setQuery(&xq, QUrl::fromLocalFile(xq.fileName()));

    QXmlResultItems res;
    query.evaluateTo(&res&#41;;
    QXmlItem item(res.next());
    while (!item.isNull()) {
      if (item.isAtomicValue()) {
        QVariant v = item.toAtomicValue();
        if(v.type() == QVariant::String){
          comando = v.toString();
          qDebug() << "getComando()" << comando;
        }
      }
      item = res.next();
    }
    

    } else
    qDebug() << "not opened" << xq.fileName();

    xq.close();
    instanceFile.close();

    return comando;
    }@

    After running this method, item is always null when it is supossed to be atomic value containing the expected string.

    1 Reply Last reply
    0
    • J Offline
      J Offline
      johnlubest
      wrote on last edited by
      #2

      I found the problem, I was giving an incorrect file name for the xml to be processed.

      I changed this line:
      @QFile instanceFile(getXMLPrincipal());@

      I put another fuction as parameter to get the path to the correct file and I got it working.

      1 Reply Last reply
      0

      • Login

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