Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. Italian
  4. QXmlQuery
QtWS25 Last Chance

QXmlQuery

Scheduled Pinned Locked Moved Italian
3 Posts 2 Posters 2.2k Views
  • 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.
  • F Offline
    F Offline
    Fabio Stafforte
    wrote on last edited by
    #1

    Buon giorno a tutti, sono nuovo a Qt.
    Ho il seguente problema, ho un file di configurazione xml diviso nel classico sezione chiave valore/ o lista di valori.
    Sto provando ad usare QXmlQuery per prendere in valore/i di una sezione/chiave forniti in input ad un metodo.
    Questo ed in file xml
    <?xml version=“1.0” encoding=“UTF-8”?>
    <logs_collector> <section name=“limits”> <data key=“MIN_MAX_LOG_LINES” type=“unsignedInteger”> <values> <value>100</value> </values> </data> <data key=“MAX_MAX_LOG_LINES” type=“unsignedInteger”> <values> <value>65536</value> </values> </data> </section>
    </logs_collector>

    e questo è il codice che ho scritto per ottenere un determinato valore

    const QVariant Configuration::getConfigValue(const QString& sectioName, const QString& keyName, const QVariant& defaultValue) const {

    QVariant result(defaultValue);
    QXmlQuery xmlQuery(QXmlQuery::XPath20);
    const QString& queryStr = configQueryStr.arg(sectioName).arg(keyName);
    QUrl url(this->xmlConfigFile); xmlQuery.setFocus(url); xmlQuery.setQuery(queryStr); if (true == xmlQuery.isValid()) { QStringList xpathResult; xmlQuery.evaluateTo(&xpathResult); // qui devo parsare il risultato per eliminare i tags xml result = xpathResult; } return result; }

    la QString parametrica che uso come query è la sguente
    static QString configQueryStr(”‘section[@name=\”%1\”]/data[@key=\”%2\”]/values/value’”);

    quando passo per esempio come nome sezione limits e come key MAX_MAX_LOG_LINES
    questa è la stringa che mi si compone prima di passarela a la classe QXmlQuery
    ‘section[@name=“limits”]/data[@key=“MAX_MAX_LOG_LINES”]/values/value’

    Attraverso il debugger ho visto che la
    il la query è valida ma nel risultato finare invece di darmi <value>65535</value> mi ritona solo la quesry stessa.
    Ho riprovato la stessa query con xmllint e funziona.
    Dove sbaglio ?
    Grazie a tutti

    1 Reply Last reply
    0
    • N Offline
      N Offline
      nologinma
      wrote on last edited by
      #2

      Prova con questa funzione:

      @QString Configuration::getConfigValue(const QString& sectioName, const QString& keyName, const QVariant& defaultValue)
      {

      QVariant result(defaultValue);
      QXmlQuery xmlQuery(QXmlQuery::XPath20);
      QString result;

      const QString& queryStr = configQueryStr.arg(sectioName).arg(keyName);

      QUrl url(this->xmlConfigFile);
      xmlQuery.setFocus(url);
      xmlQuery.setQuery(queryStr);

      if (true == xmlQuery.isValid())
      {
      QBuffer outb;
      outb.open(QIODevice::ReadWrite);
      QXmlSerializer serializer(xmlQuery, &outb);
      xmlQuery.evaluateTo(&serializer);

      QXmlItem item(serializer.next());

      while (!item.isNull())
      {
      if (item.isNode())
      {
      result.append(item);
      }
      item = result.next();
      }

      return result;
      }
      return result;
      } @

      1 Reply Last reply
      0
      • F Offline
        F Offline
        Fabio Stafforte
        wrote on last edited by
        #3

        Sorry if I am answering now but a QxmlSerializer does not have a method next ... so how can QXmlItem item(serializer.next()); work if the compiler give me the relevant error ?

        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