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. Reading a xml file and parsing it
Forum Update on Monday, May 27th 2025

Reading a xml file and parsing it

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 3 Posters 5.1k 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.
  • A Offline
    A Offline
    Aashu10
    wrote on 26 Nov 2015, 12:02 last edited by Aashu10
    #1

    I am not that good with xml, but my basic xml file looks somthing like this.

    XML CODE:

    <MAIN_HEADER>
    
      <HEADER>
        <TITLE>my_title</TITLE>
        <AUTOR>DNL</AUTOR>
        <NAME>John</NAME>
        <AGE>abc</AGE>
        <SEX>male</SEX>
        <PLACE>abc</PLACE>
        <INI_FILE>abc</INI_FILE>
    
      </HEADER>
    

    what I want to do is, I need to find 2-3 tags, say for example NAME & SEX and store the attribute(John,Male) in another variable.

    until now, I have been able to make it read the xml file.

    void MainWindow::XMLParser()
    {
            QString path=MainWindow::getWorkingDirectory()+"\\0_Config\\";
            QString string;
            string = path + ui->ConfigFiles_combo->currentText();
            QXmlStreamReader xmlreader;
            QFile file(string);
            if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
            {
               QMessageBox::information(this,tr("info"),tr("Failed to open file"));
            }
            else
            {
                qDebug()<<"ok";
            }
    
    
            while(!xmlreader.atEnd())
               {
                   if(xmlreader.isStartElement())
                   {
                       if(xmlreader.name()== "NEOTUX_CFG")
                       {
                           xmlreader.readNext();
                       }
                       else  if(xmlreader.name() == "HEADER")
                       {
                         while(!xmlreader.atEnd())
                           {
                              if(xmlreader.isEndElement())
                               {
                                 if(xmlreader.name() == "TITLE")
                                  {
                                    xmlreader.readNext();
                                    break;
                                   }
                                   xmlreader.readNext();
                               }
                              else if(xmlreader.isCharacters())
                               {
                                  xmlreader.readNext();
                               }
                              else if(xmlreader.isStartElement())
                               {
                                 if(xmlreader.name() == "PLATFORM")
                                   {
                                     xmlreader.readNext();
                                   }
                                  else if(xmlreader.name() == "AUTHOR")
                                  {
                                   xmlreader.readNext();
                                  }
                                  else if(xmlreader.name() == "TEST_VARIANT")
                                  {
                                   QString name=xmlreader.readElementText(); // found
                                   qDebug()<< name;
                                  }
    
    
                              }
                               else
                               {
                                 xmlreader.readNext();
                               }
                           }
    
                   if(xmlreader.isEndElement())
                   {
                       if(xmlreader.name() == "HEADER")
                       {
                           break;
                       }
                       xmlreader.readNext();
                   }
                   else
                   {
                       xmlreader.readNext();
                   }
               }
    
            }
    
    
    }
            }
    

    how do I make it search for the exact tag and store it inside another variable?

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Ratzz
      wrote on 26 Nov 2015, 12:10 last edited by Ratzz
      #2

      Hi and welcome,
      You can use isStartElement() to read the start of the tag. name() to match exact name.
      writeStartElement to start write the tag header and writeTextElementto write a text element .

      1 Reply Last reply
      1
      • A Offline
        A Offline
        Aashu10
        wrote on 26 Nov 2015, 12:17 last edited by
        #3

        Hey!

        I do not want to write anything on the XML, I need to fetch the attribute of that tag so that I can use it for later. Can you please show me some example which is related to my case. because every where I see I get examples of XmlStreamRreader.

        R 1 Reply Last reply 26 Nov 2015, 13:03
        0
        • R Offline
          R Offline
          Ratzz
          wrote on 26 Nov 2015, 12:39 last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • A Aashu10
            26 Nov 2015, 12:17

            Hey!

            I do not want to write anything on the XML, I need to fetch the attribute of that tag so that I can use it for later. Can you please show me some example which is related to my case. because every where I see I get examples of XmlStreamRreader.

            R Offline
            R Offline
            Ratzz
            wrote on 26 Nov 2015, 13:03 last edited by Ratzz
            #5

            @Aashu10
            You can read this way.

            qxmlstreamreader *xmlreader;
            
            while(!xmlreader->atEnd())
               {
                   if(xmlreader->isStartElement())
                   {
                       if(xmlreader->name()== "MAIN_HEADER")
                       {
                           xmlreader->readnext();
                       }
               		else  if(xmlreader->name() == "HEADER")
               		{
                         while(!xmlreader->atEnd())
                           {
                              if(xmlreader->isendelement())
                               {
                                 if(xmlreader->name() == "HEADER")
                                  {
                                    xmlreader->readNext();
                                    break;
                                   }
                                   xmlreader->readNext();
                               }
                              else if(xmlreader->isCharacters())
                               {
                                  xmlreader->readnext();
                               }
                              else if(xmlreader->isStartElement())
                               {
                                 if(xmlreader->name() == "TITLE")
                                   {            
                                     xmlreader->readNext();
                                   }
                                  else if(xmlreader->name() == "AUTHOR")
                                  {
                                   xmlreader->readNext();
                                  }
                                  else if(xmlreader->name() == "NAME")
               		           {
               		            qstring name=xmlReader->readElementText()) ;// found
               		           }
               		          .................
               		
                               else
                               {
                                 xmlreader->readNext();
                               }
               			}
               	
                   else if(xmlreader->isEndElement())
                   {
                       if(xmlreader->name() == "MAIN_HEADER")
                       {
                           break;
                       }
                       xmlreader->readNext();
                   }
                   else
                   {
                       xmlreader->readnext();
                   }
               }
            
            }		
            
            
            1 Reply Last reply
            1
            • A Offline
              A Offline
              Aashu10
              wrote on 26 Nov 2015, 13:19 last edited by
              #6

              So i do not have to consider the main tag? <MAIN_HEADER>

              R 1 Reply Last reply 26 Nov 2015, 13:24
              0
              • A Aashu10
                26 Nov 2015, 13:19

                So i do not have to consider the main tag? <MAIN_HEADER>

                R Offline
                R Offline
                Ratzz
                wrote on 26 Nov 2015, 13:24 last edited by
                #7

                @Aashu10
                I have EDITed the previous post.

                1 Reply Last reply
                1
                • A Offline
                  A Offline
                  Aashu10
                  wrote on 26 Nov 2015, 13:37 last edited by
                  #8

                  I am very new to Xmlstreamreader, Please bear with me
                  I am getting a error as " C2039: 'readnext' : is not a member of 'QXmlStreamReader'

                  R 1 Reply Last reply 26 Nov 2015, 13:41
                  0
                  • A Aashu10
                    26 Nov 2015, 13:37

                    I am very new to Xmlstreamreader, Please bear with me
                    I am getting a error as " C2039: 'readnext' : is not a member of 'QXmlStreamReader'

                    R Offline
                    R Offline
                    Ratzz
                    wrote on 26 Nov 2015, 13:41 last edited by
                    #9

                    @Aashu10
                    I edited the code in the text editor. So be careful with the spelling.

                    xmlReader->readNext();
                    
                    1 Reply Last reply
                    1
                    • A Offline
                      A Offline
                      Aashu10
                      wrote on 26 Nov 2015, 13:52 last edited by Aashu10
                      #10

                      Ah there's sytax error, trying real hard to find it out. I'm getting:
                      " error: C2181: illegal else without matching if"
                      on line:
                      else if(xmlreader.isEndElement()). any idea why?

                      R 1 Reply Last reply 26 Nov 2015, 14:03
                      0
                      • jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 26 Nov 2015, 13:56 last edited by
                        #11

                        Probably } before else if(xmlreader.isEndElement()) is missing

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • A Aashu10
                          26 Nov 2015, 13:52

                          Ah there's sytax error, trying real hard to find it out. I'm getting:
                          " error: C2181: illegal else without matching if"
                          on line:
                          else if(xmlreader.isEndElement()). any idea why?

                          R Offline
                          R Offline
                          Ratzz
                          wrote on 26 Nov 2015, 14:03 last edited by
                          #12

                          @Aashu10
                          We cannot just say if the tag is read or not. it requires careful observations.
                          Best way is to right the read tags back to .xml file to check if it really read or not .

                          1 Reply Last reply
                          1
                          • A Offline
                            A Offline
                            Aashu10
                            wrote on 26 Nov 2015, 14:16 last edited by
                            #13

                            @Ratzz

                            Thanks a ton! i think i am near to solving the problem, i have edited the program. You can see what exactly i am doing. I choosing a xml file from a dropdown. and QT parse that XML file. Now when i choose an xml file. Whole program gets stuck.

                            R 1 Reply Last reply 26 Nov 2015, 14:18
                            0
                            • A Aashu10
                              26 Nov 2015, 14:16

                              @Ratzz

                              Thanks a ton! i think i am near to solving the problem, i have edited the program. You can see what exactly i am doing. I choosing a xml file from a dropdown. and QT parse that XML file. Now when i choose an xml file. Whole program gets stuck.

                              R Offline
                              R Offline
                              Ratzz
                              wrote on 26 Nov 2015, 14:18 last edited by Ratzz
                              #14

                              @Aashu10
                              I have written a test code which works fine for me . Its here .or from here .

                              1 Reply Last reply
                              1
                              • A Offline
                                A Offline
                                Aashu10
                                wrote on 26 Nov 2015, 14:36 last edited by Aashu10
                                #15

                                Thanks alot! still can't figure out why does my gui hangs.
                                Can you please tell me what does String holds in:
                                "QString name=xmlreader.readElementText(); // found"

                                I had a debug there to check, unfortunately my gui is hanging

                                R 1 Reply Last reply 27 Nov 2015, 04:39
                                0
                                • A Aashu10
                                  26 Nov 2015, 14:36

                                  Thanks alot! still can't figure out why does my gui hangs.
                                  Can you please tell me what does String holds in:
                                  "QString name=xmlreader.readElementText(); // found"

                                  I had a debug there to check, unfortunately my gui is hanging

                                  R Offline
                                  R Offline
                                  Ratzz
                                  wrote on 27 Nov 2015, 04:39 last edited by Ratzz
                                  #16

                                  Did you try the test code?
                                  QString name holds the name john
                                  Download this zip file. Build your project and browse for the XML.xml file to find the matched name .

                                  1 Reply Last reply
                                  1
                                  • A Offline
                                    A Offline
                                    Aashu10
                                    wrote on 27 Nov 2015, 06:53 last edited by
                                    #17

                                    Thanks for the help! this works too! But i think DOM is turning out to be simpler, i really appriciate your help. don't exactly know how to accept your answer on this forum.

                                    R 1 Reply Last reply 27 Nov 2015, 06:58
                                    0
                                    • A Aashu10
                                      27 Nov 2015, 06:53

                                      Thanks for the help! this works too! But i think DOM is turning out to be simpler, i really appriciate your help. don't exactly know how to accept your answer on this forum.

                                      R Offline
                                      R Offline
                                      Ratzz
                                      wrote on 27 Nov 2015, 06:58 last edited by Ratzz
                                      #18

                                      @Aashu10
                                      There is nothing like "answer" for particular question . You can Upvote the answer(s) that helped you to solve the issue.
                                      Once your problem is solved please use the Topic Tools button to mark as Solved.
                                      Thank you!

                                      1 Reply Last reply
                                      1

                                      1/18

                                      26 Nov 2015, 12:02

                                      • Login

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