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. why no findXmlElement functions?
Forum Updated to NodeBB v4.3 + New Features

why no findXmlElement functions?

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 5 Posters 1.1k 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.
  • F Offline
    F Offline
    fryn3
    wrote on last edited by fryn3
    #1

    why no findXmlElement functions?

    class MyXmlStreamReader : public QXmlStreamReader
    {
    public:
        MyXmlStreamReader() : QXmlStreamReader() { }
        MyXmlStreamReader(QIODevice *device) : QXmlStreamReader(device) { }
        MyXmlStreamReader(const QByteArray &data) : QXmlStreamReader(data) { }
        MyXmlStreamReader(const QString &data) : QXmlStreamReader(data) { }
        MyXmlStreamReader(const char * data) : QXmlStreamReader(data) { }
        bool findXmlElement(QStringList path) {
            if (path.isEmpty()) {
                return false;
            }
            QString findName = path.at(0);
            while (!atEnd() && !hasError()) {
                readNextStartElement();
                if (findName == name()) {
                    if (path.size() == 1) {
                        return true;
                    } else {
                        return findXmlElement(path.mid(1));
                    }
                } else {
                    skipCurrentElement();
                }
            }
            return false;
        }
        bool findXmlElement(QList<QRegExp> path) { return false; }   // not implemented
    };
    

    Example
    xml-file

    <?xml version="1.0" encoding="UTF-16" ?>
    <persons>
        <person id="1">
            <firstname>John</firstname>
            <surname>Doe</surname>
            <email>john.doe@example.com</email>
            <website>http://en.wikipedia.org/wiki/John_Doe</website>
        </person>
        <person id="2">
            <firstname>Jane</firstname>
            <surname>Doe</surname>
            <email>jane.doe@example.com</email>
            <website>http://en.wikipedia.org/wiki/John_Doe</website>
        </person>
        <person id="3">
            <firstname>Matti</firstname>
            <surname>Meikäläinen</surname>
            <email>matti.meikalainen@example.com</email>
            <website>http://fi.wikipedia.org/wiki/Matti_Meikäläinen</website>
        </person>
    </persons>
    

    code

    int main (int argc, char *argv[]) {
        QApplication a(argc, argv);
        QFile f("file.xml");
        f.open(QIODevice::ReadOnly);
        MyXmlStreamReader xml(&f);
        qDebug() << xml.findXmlElement(QStringList() << "persons" << "person" << "email");
        qDebug() << xml.tokenString() << xml.name() << xml.text();
        return 0;
    }
    
    JonBJ 1 Reply Last reply
    0
    • F fryn3

      why no findXmlElement functions?

      class MyXmlStreamReader : public QXmlStreamReader
      {
      public:
          MyXmlStreamReader() : QXmlStreamReader() { }
          MyXmlStreamReader(QIODevice *device) : QXmlStreamReader(device) { }
          MyXmlStreamReader(const QByteArray &data) : QXmlStreamReader(data) { }
          MyXmlStreamReader(const QString &data) : QXmlStreamReader(data) { }
          MyXmlStreamReader(const char * data) : QXmlStreamReader(data) { }
          bool findXmlElement(QStringList path) {
              if (path.isEmpty()) {
                  return false;
              }
              QString findName = path.at(0);
              while (!atEnd() && !hasError()) {
                  readNextStartElement();
                  if (findName == name()) {
                      if (path.size() == 1) {
                          return true;
                      } else {
                          return findXmlElement(path.mid(1));
                      }
                  } else {
                      skipCurrentElement();
                  }
              }
              return false;
          }
          bool findXmlElement(QList<QRegExp> path) { return false; }   // not implemented
      };
      

      Example
      xml-file

      <?xml version="1.0" encoding="UTF-16" ?>
      <persons>
          <person id="1">
              <firstname>John</firstname>
              <surname>Doe</surname>
              <email>john.doe@example.com</email>
              <website>http://en.wikipedia.org/wiki/John_Doe</website>
          </person>
          <person id="2">
              <firstname>Jane</firstname>
              <surname>Doe</surname>
              <email>jane.doe@example.com</email>
              <website>http://en.wikipedia.org/wiki/John_Doe</website>
          </person>
          <person id="3">
              <firstname>Matti</firstname>
              <surname>Meikäläinen</surname>
              <email>matti.meikalainen@example.com</email>
              <website>http://fi.wikipedia.org/wiki/Matti_Meikäläinen</website>
          </person>
      </persons>
      

      code

      int main (int argc, char *argv[]) {
          QApplication a(argc, argv);
          QFile f("file.xml");
          f.open(QIODevice::ReadOnly);
          MyXmlStreamReader xml(&f);
          qDebug() << xml.findXmlElement(QStringList() << "persons" << "person" << "email");
          qDebug() << xml.tokenString() << xml.name() << xml.text();
          return 0;
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @fryn3 said in why no findXmlElement functions?:

      why no findXmlElement functions?

      What sort of an answer are you expecting to this? QXmlStreamReader does not provide "find" functions, it's a forward-only fast stream reader. Use https://doc.qt.io/qt-5/qdomdocument.html (e.g. elementsByTagName()) if you want more advanced capabilities after reading in a whole document. Otherwise you are are writing your own findXmlElement() methods, so what's the issue?

      F 1 Reply Last reply
      2
      • JonBJ JonB

        @fryn3 said in why no findXmlElement functions?:

        why no findXmlElement functions?

        What sort of an answer are you expecting to this? QXmlStreamReader does not provide "find" functions, it's a forward-only fast stream reader. Use https://doc.qt.io/qt-5/qdomdocument.html (e.g. elementsByTagName()) if you want more advanced capabilities after reading in a whole document. Otherwise you are are writing your own findXmlElement() methods, so what's the issue?

        F Offline
        F Offline
        fryn3
        wrote on last edited by fryn3
        #3

        @JonB said in why no findXmlElement functions?:

        Use https://doc.qt.io/qt-5/qdomdocument.html (e.g. elementsByTagName()) if you want more advanced capabilities after reading in a whole document.

        The module is not actively maintained anymore. Please use the QXmlStreamReader and QXmlStreamWriter classes in Qt Core instead.

        Later, I will prepare a detailed answer.

        JonBJ 1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          efficiency point:

           bool findXmlElement(QStringList path){return findXmlElement(path.cbegin(),path.cend());}
          private:
           bool findXmlElement(QStringList::const_iterator pathBegin,QStringList::const_iterator pathEnd) {
                  if (pathBegin==pathEnd)
                      return false;
                  while (!atEnd() && !hasError()) {
                      readNextStartElement();
                      if (*pathBegin== name()) {
                          if (++pathBegin == pathEnd)
                              return true;
                              return findXmlElement(pathBegin ,pathEnd);
                      } else {
                          skipCurrentElement();
                      }
                  }
                  return false;
              }
          

          Functionality point:
          If you have

          <person id="1">
                  <firstname>John</firstname>
                  <surname>Doe</surname>
                  <email>john.doe@example.com</email>
                  <website>http://en.wikipedia.org/wiki/John_Doe</website>
              </person>
              <person id="2">
                  <firstname>Jane</firstname>
                  <surname>Doe</surname>
                 <middlename>James</middlename>
                  <email>jane.doe@example.com</email>
                  <website>http://en.wikipedia.org/wiki/John_Doe</website>
              </person>
          

          Your algorithm will not be able to find QStringList{"person","middlename"}

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          F 1 Reply Last reply
          2
          • VRoninV VRonin

            efficiency point:

             bool findXmlElement(QStringList path){return findXmlElement(path.cbegin(),path.cend());}
            private:
             bool findXmlElement(QStringList::const_iterator pathBegin,QStringList::const_iterator pathEnd) {
                    if (pathBegin==pathEnd)
                        return false;
                    while (!atEnd() && !hasError()) {
                        readNextStartElement();
                        if (*pathBegin== name()) {
                            if (++pathBegin == pathEnd)
                                return true;
                                return findXmlElement(pathBegin ,pathEnd);
                        } else {
                            skipCurrentElement();
                        }
                    }
                    return false;
                }
            

            Functionality point:
            If you have

            <person id="1">
                    <firstname>John</firstname>
                    <surname>Doe</surname>
                    <email>john.doe@example.com</email>
                    <website>http://en.wikipedia.org/wiki/John_Doe</website>
                </person>
                <person id="2">
                    <firstname>Jane</firstname>
                    <surname>Doe</surname>
                   <middlename>James</middlename>
                    <email>jane.doe@example.com</email>
                    <website>http://en.wikipedia.org/wiki/John_Doe</website>
                </person>
            

            Your algorithm will not be able to find QStringList{"person","middlename"}

            F Offline
            F Offline
            fryn3
            wrote on last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • F fryn3

              @JonB said in why no findXmlElement functions?:

              Use https://doc.qt.io/qt-5/qdomdocument.html (e.g. elementsByTagName()) if you want more advanced capabilities after reading in a whole document.

              The module is not actively maintained anymore. Please use the QXmlStreamReader and QXmlStreamWriter classes in Qt Core instead.

              Later, I will prepare a detailed answer.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @fryn3

              The module is not actively maintained anymore. Please use the QXmlStreamReader and QXmlStreamWriter classes in Qt Core instead.

              I don't know if this is something aimed at me? Whatever link you put on "The module" in what you wrote doesn't lead anywhere?

              ODБOïO 1 Reply Last reply
              0
              • JonBJ JonB

                @fryn3

                The module is not actively maintained anymore. Please use the QXmlStreamReader and QXmlStreamWriter classes in Qt Core instead.

                I don't know if this is something aimed at me? Whatever link you put on "The module" in what you wrote doesn't lead anywhere?

                ODБOïO Offline
                ODБOïO Offline
                ODБOï
                wrote on last edited by
                #7

                @JonB https://doc.qt.io/QT-5/qtxml-module.html

                JonBJ 1 Reply Last reply
                0
                • ODБOïO ODБOï

                  @JonB https://doc.qt.io/QT-5/qtxml-module.html

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @LeLev
                  Oh! You mean that all those QXml... and QDom... classes are being withdrawn? No support for reading your entire XML document into a DOM and searching/editing it from there? Seriously? Seems like a retrograde step, Qt provides loads of useful stuff, does it not need such slightly higher-level support for XML?

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    QtXml is in 'done' state - this doesn't mean it will be removed. It just will not gain any major updates anymore.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    4
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #10

                      Just to give you a comparison, bzip2 hasn't received a major update since 2010 and zlib hasn't received any update for more than2 years but they are still the most used compression libraries i can think of. The Qt xml module entered the same stage, it implemented all the features that were considered "in scope" and it woks well as is

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      1 Reply Last reply
                      3
                      • JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #11

                        Good to know. In which case, although the OP rejected my suggestion that he could choose to read the whole document in and do things like QDomDocument::elementsByTagName() because "The module is not actively maintained anymore", that is still an acceptable approach going forward, right?

                        1 Reply Last reply
                        0
                        • JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #12

                          BTW, I've just searched and am delighted to see that Qt does have XPath/XQuery, https://doc.qt.io/qt-5/xmlprocessing.html. It's up to the OP, but I will say that using XPath queries on your XML documents is an order of magnitude nicer than walking the tree in code.

                          And for quite separate purposes, I see that also has (at least some) XSLT support, so it gets better & better... :)

                          1 Reply Last reply
                          1

                          • Login

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