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 794 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.
  • M Offline
    M Offline
    Mery_lamb
    wrote on 5 Jan 2021, 15:34 last edited by
    #1

    Hello;

    Please find below the code (I found in a website) and I use it to parse the xml file

    
    test1_number  = 0; 
    if (reader.readNextStartElement()) {
            if (reader.name() == "root"){
                while(reader.readNextStartElement()){
                    if(reader.name() == "FirstChild"){
                        while(reader.readNextStartElement()){
                            if(reader.name() == "test1"){
                                qDebug << "Hello Test1 \n"; 
                               test1_number ++ ; 
                            }
                            else
                                reader.skipCurrentElement();
                        }
                    }
                    else
                        reader.skipCurrentElement();
                }
            }
            else
                reader.raiseError(QObject::tr("Incorrect file"));
    }
    
    

    xml file :

    <?xml version="1.0" encoding="UTF-8"?>
    <!--example with no DTD-->
    <root>
       <FirstChild>
    		<test1 type= "girl"> 
    			<name> asmaa </name>
    			<age> 14</age>
    		</test1>
                    
    		<test1 type= "boy"> 
    			<name> boy </name>
    			<age> 10</age>
    		</test1>
       </FirstChild>
       <SecondChild>text3</SecondChild>
    </root>            
    

    I want to count number of test1 Item in the xml file but I always get 1 even if we have more than that in the file

    Can anyone help me please

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 6 Jan 2021, 12:23 last edited by SGaist 1 Jun 2021, 12:24
      #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 6 Jan 2021, 12:38
      2
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 5 Jan 2021, 19:35 last edited by
        #2

        Hi,

        Are you following this one ?

        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 5 Jan 2021, 20:05
        0
        • S SGaist
          5 Jan 2021, 19:35

          Hi,

          Are you following this one ?

          M Offline
          M Offline
          Mery_lamb
          wrote on 5 Jan 2021, 20:05 last edited by
          #3

          @SGaist Yes I did I followed the second one

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 5 Jan 2021, 21:31 last edited by
            #4

            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.

            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 6 Jan 2021, 09:04
            2
            • S SGaist
              5 Jan 2021, 21:31

              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 6 Jan 2021, 09:04 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 S 2 Replies Last reply 6 Jan 2021, 09:25
              0
              • M Mery_lamb
                6 Jan 2021, 09:04

                @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 Offline
                JonBJ Offline
                JonB
                wrote on 6 Jan 2021, 09:25 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 6 Jan 2021, 11:17
                1
                • M Mery_lamb
                  6 Jan 2021, 09:04

                  @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();
                  
                  S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 6 Jan 2021, 09:27 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 6 Jan 2021, 09:48
                  1
                  • S SGaist
                    6 Jan 2021, 09:27

                    @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 6 Jan 2021, 09:48 last edited by
                    #8
                    This post is deleted!
                    1 Reply Last reply
                    0
                    • JonBJ JonB
                      6 Jan 2021, 09:25

                      @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 6 Jan 2021, 11:17 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 6 Jan 2021, 12:12
                      0
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 6 Jan 2021, 12:08 last edited by SGaist 1 Jun 2021, 12:09
                        #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 6 Jan 2021, 12:11
                        0
                        • S SGaist
                          6 Jan 2021, 12:08

                          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 6 Jan 2021, 12:11 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
                            6 Jan 2021, 11:17

                            @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 Offline
                            JonBJ Offline
                            JonB
                            wrote on 6 Jan 2021, 12:12 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
                            • S Offline
                              S Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on 6 Jan 2021, 12:23 last edited by SGaist 1 Jun 2021, 12:24
                              #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 6 Jan 2021, 12:38
                              2
                              • S SGaist
                                6 Jan 2021, 12:23

                                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 6 Jan 2021, 12:38 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

                                1/14

                                5 Jan 2021, 15:34

                                • Login

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