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. Parsing XML file with QXmlStreamReader
Forum Updated to NodeBB v4.3 + New Features

Parsing XML file with QXmlStreamReader

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 3 Posters 816 Views 2 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.
  • SGaistS SGaist

    I think you are missing a "reader.skipCurrentElement();" after your counter update.

    You have more data under test1 and these you do not seem to want to parse. You current code will continue getting name and age and then stop since there are no more start element after it.

    M Offline
    M Offline
    Mery_lamb
    wrote on last edited by
    #5

    @SGaist but after the counter update I add a reader.skipCurrentElement(). please take a look to this part

    if(reader.name() == "FirstChild"){
                        while(reader.readNextStartElement()){
                            if(reader.name() == "test1"){
                                qDebug << "Hello Test1 \n"; 
                               test1_number ++ ; 
                            }
                            else
                                reader.skipCurrentElement();
                        }
                    }
                    else
                        reader.skipCurrentElement();
    
    JonBJ SGaistS 2 Replies Last reply
    0
    • M Mery_lamb

      @SGaist but after the counter update I add a reader.skipCurrentElement(). please take a look to this part

      if(reader.name() == "FirstChild"){
                          while(reader.readNextStartElement()){
                              if(reader.name() == "test1"){
                                  qDebug << "Hello Test1 \n"; 
                                 test1_number ++ ; 
                              }
                              else
                                  reader.skipCurrentElement();
                          }
                      }
                      else
                          reader.skipCurrentElement();
      
      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #6

      @Mery_lamb
      Debug out reader.name() at every stage to see where your reader is getting to.

      M 1 Reply Last reply
      1
      • M Mery_lamb

        @SGaist but after the counter update I add a reader.skipCurrentElement(). please take a look to this part

        if(reader.name() == "FirstChild"){
                            while(reader.readNextStartElement()){
                                if(reader.name() == "test1"){
                                    qDebug << "Hello Test1 \n"; 
                                   test1_number ++ ; 
                                }
                                else
                                    reader.skipCurrentElement();
                            }
                        }
                        else
                            reader.skipCurrentElement();
        
        SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #7

        @Mery_lamb said in Parsing XML file with QXmlStreamReader:

        @SGaist but after the counter update I add a reader.skipCurrentElement(). please take a look to this part

        if(reader.name() == "FirstChild"){
                            while(reader.readNextStartElement()){
                                if(reader.name() == "test1"){
                                    qDebug << "Hello Test1 \n"; 
                                   test1_number ++ ; 
                                }
                                else
                                    reader.skipCurrentElement();
                            }
                        }
                        else
                            reader.skipCurrentElement();
        

        No you did not, it's in the else clause.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        M 1 Reply Last reply
        1
        • SGaistS SGaist

          @Mery_lamb said in Parsing XML file with QXmlStreamReader:

          @SGaist but after the counter update I add a reader.skipCurrentElement(). please take a look to this part

          if(reader.name() == "FirstChild"){
                              while(reader.readNextStartElement()){
                                  if(reader.name() == "test1"){
                                      qDebug << "Hello Test1 \n"; 
                                     test1_number ++ ; 
                                  }
                                  else
                                      reader.skipCurrentElement();
                              }
                          }
                          else
                              reader.skipCurrentElement();
          

          No you did not, it's in the else clause.

          M Offline
          M Offline
          Mery_lamb
          wrote on last edited by
          #8
          This post is deleted!
          1 Reply Last reply
          0
          • JonBJ JonB

            @Mery_lamb
            Debug out reader.name() at every stage to see where your reader is getting to.

            M Offline
            M Offline
            Mery_lamb
            wrote on last edited by
            #9

            @JonB I did and I found that inside the if(reader.name() == "test1"), I get only one element which mean that maaaaybe I forget reader.skipCurrentElement() but I am not sure where to add it

            JonBJ 1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by SGaist
              #10

              right after test1_number++.

              Or just remove the else of that specific if since you are not doing any other processing of what is inside that element.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              M 1 Reply Last reply
              0
              • SGaistS SGaist

                right after test1_number++.

                Or just remove the else of that specific if since you are not doing any other processing of what is inside that element.

                M Offline
                M Offline
                Mery_lamb
                wrote on last edited by
                #11

                @SGaist
                If I remove the else after the counter it will not show "test1 " at all. if I add the reader.skipCurrentElement() after the counter I get the same result

                1 Reply Last reply
                0
                • M Mery_lamb

                  @JonB I did and I found that inside the if(reader.name() == "test1"), I get only one element which mean that maaaaybe I forget reader.skipCurrentElement() but I am not sure where to add it

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by
                  #12

                  @Mery_lamb
                  I haven't looked at it/used this XML reader code. But I think you'll find that after you read the <test1 type= "girl"> you still have go down into the <name> asmaa </name>, <age> 14</age> stuff, and then you have to come out again to move onto the <test1 type= "boy"> .

                  Anyway, I wouldn't know without trying it myself.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by SGaist
                    #13

                    This works as expected:

                    #include <QCoreApplication>
                    #include <QFile>
                    #include <QXmlStreamReader>
                    #include <QtDebug>
                    
                    int main(int argc, char **argv)
                    {
                        QCoreApplication app(argc, argv);
                    
                        QFile file("test.xml");
                        if(!file.open(QFile::ReadOnly | QFile::Text)){
                            qDebug() << "Cannot read file" << file.errorString();
                            exit(0);
                        }
                    
                        QXmlStreamReader reader(&file);
                    
                        int test1_number  = 0;
                        if (reader.readNextStartElement()) {
                            if (reader.name() == "root" ){
                                while(reader.readNextStartElement()) {
                                    qDebug() << "First level start element name" << reader.name();
                                    if(reader.name() == "FirstChild") {
                                        while(reader.readNextStartElement()) {
                                            qDebug() << "Second level start element name" << reader.name();
                                            if(reader.name() == "test1"){
                                                qDebug() << "Hello Test1 \n";
                                                test1_number++ ;
                                            }
                    
                                            reader.skipCurrentElement();
                                        }
                                    } else {
                                        reader.skipCurrentElement();
                                    }
                                }
                            } else {
                                reader.raiseError(QObject::tr("Incorrect file"));
                            }
                        }
                    
                        qDebug() << "Number of 'test1'" << test1_number;
                    
                        return 0;
                    }
                    

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    M 1 Reply Last reply
                    2
                    • SGaistS SGaist

                      This works as expected:

                      #include <QCoreApplication>
                      #include <QFile>
                      #include <QXmlStreamReader>
                      #include <QtDebug>
                      
                      int main(int argc, char **argv)
                      {
                          QCoreApplication app(argc, argv);
                      
                          QFile file("test.xml");
                          if(!file.open(QFile::ReadOnly | QFile::Text)){
                              qDebug() << "Cannot read file" << file.errorString();
                              exit(0);
                          }
                      
                          QXmlStreamReader reader(&file);
                      
                          int test1_number  = 0;
                          if (reader.readNextStartElement()) {
                              if (reader.name() == "root" ){
                                  while(reader.readNextStartElement()) {
                                      qDebug() << "First level start element name" << reader.name();
                                      if(reader.name() == "FirstChild") {
                                          while(reader.readNextStartElement()) {
                                              qDebug() << "Second level start element name" << reader.name();
                                              if(reader.name() == "test1"){
                                                  qDebug() << "Hello Test1 \n";
                                                  test1_number++ ;
                                              }
                      
                                              reader.skipCurrentElement();
                                          }
                                      } else {
                                          reader.skipCurrentElement();
                                      }
                                  }
                              } else {
                                  reader.raiseError(QObject::tr("Incorrect file"));
                              }
                          }
                      
                          qDebug() << "Number of 'test1'" << test1_number;
                      
                          return 0;
                      }
                      
                      M Offline
                      M Offline
                      Mery_lamb
                      wrote on last edited by
                      #14

                      @SGaist thank you for your help I was stuck in this error from last 3 days. it seems like I need to take another look to my Qt notes.

                      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