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. Parse an Xml file with Qt
Forum Updated to NodeBB v4.3 + New Features

Parse an Xml file with Qt

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 3.3k 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.
  • B Offline
    B Offline
    Bogdan112
    wrote on 11 Feb 2017, 15:34 last edited by
    #1

    Hey,
    I am trying to parse an xml file and to save all the output in a QString vector.
    I don't know why but it add same elements like : "\n", "\n " and same blank space:
    Here is the code:

     QFile* file = new QFile("new_ex4.xml");
        if (!file->open(QIODevice::ReadOnly | QIODevice::Text))
        {
            QMessageBox::information(0, "error", file->errorString());
             return;
        }
        QVector<QString> vector;
        QXmlStreamReader xml(file);
        QXmlStreamReader::TokenType token;
        while(!xml.atEnd() && !xml.hasError())
        {
            /* Read next element.*/
            token = xml.readNext();
            /* If token is just StartDocument, we'll go to next.*/
            if(token == QXmlStreamReader::StartDocument)
                continue;
    
         if(token == QXmlStreamReader::Characters)
                vector.push_back(xml.text().toString());
         
         continue;
        }
    

    And the xml file looks like this :

    <?xml version="1.0" encoding="UTF-8" ?>
    <test>
        <Page1>
            <name1>stringHere</name1>
            <name2>stringHere</name2>
            <name3>stringHere</name3>
            <name4>stringHere</name4>
        </Page1>
        <Page2>
            <Val1>10.256</Val1>
            <Val2>101.2</Val2>
            <Val3>101.2</Val3>
            <Val4>101.2</Val4>
        </Page2>
        <Page3>
            <Val1>10.256</Val1>
            <Val2>101.2</Val2>
            <Val3>101.2</Val3>
            <Val4>101.2</Val4>
        </Page3>
        <Page4>
            <Val1>10.256</Val1>
            <Val2>101.2</Val2>
            <Val3>101.2</Val3>
            <Val4>101.2</Val4>
        </Page4>
        <Page5>
            <Val1>10.256</Val1>
            <Val2>101.2</Val2>
            <Val3>101.2</Val3>
            <Val4>101.2</Val4>
        </Page5>
    </test>
    
    
    K 1 Reply Last reply 11 Feb 2017, 17:56
    0
    • B Bogdan112
      11 Feb 2017, 15:34

      Hey,
      I am trying to parse an xml file and to save all the output in a QString vector.
      I don't know why but it add same elements like : "\n", "\n " and same blank space:
      Here is the code:

       QFile* file = new QFile("new_ex4.xml");
          if (!file->open(QIODevice::ReadOnly | QIODevice::Text))
          {
              QMessageBox::information(0, "error", file->errorString());
               return;
          }
          QVector<QString> vector;
          QXmlStreamReader xml(file);
          QXmlStreamReader::TokenType token;
          while(!xml.atEnd() && !xml.hasError())
          {
              /* Read next element.*/
              token = xml.readNext();
              /* If token is just StartDocument, we'll go to next.*/
              if(token == QXmlStreamReader::StartDocument)
                  continue;
      
           if(token == QXmlStreamReader::Characters)
                  vector.push_back(xml.text().toString());
           
           continue;
          }
      

      And the xml file looks like this :

      <?xml version="1.0" encoding="UTF-8" ?>
      <test>
          <Page1>
              <name1>stringHere</name1>
              <name2>stringHere</name2>
              <name3>stringHere</name3>
              <name4>stringHere</name4>
          </Page1>
          <Page2>
              <Val1>10.256</Val1>
              <Val2>101.2</Val2>
              <Val3>101.2</Val3>
              <Val4>101.2</Val4>
          </Page2>
          <Page3>
              <Val1>10.256</Val1>
              <Val2>101.2</Val2>
              <Val3>101.2</Val3>
              <Val4>101.2</Val4>
          </Page3>
          <Page4>
              <Val1>10.256</Val1>
              <Val2>101.2</Val2>
              <Val3>101.2</Val3>
              <Val4>101.2</Val4>
          </Page4>
          <Page5>
              <Val1>10.256</Val1>
              <Val2>101.2</Val2>
              <Val3>101.2</Val3>
              <Val4>101.2</Val4>
          </Page5>
      </test>
      
      
      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 11 Feb 2017, 17:56 last edited by kshegunov 2 Nov 2017, 17:59
      #2

      You're half way there. QXmlStreamReader is in fact a glorified tokenizer, it's not really a parser. You need to do the parsing yourself. So what you get is expected - you receive the character tokens that are between the tags.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      2
      • B Offline
        B Offline
        Bogdan112
        wrote on 12 Feb 2017, 06:23 last edited by
        #3

        But if i receive only the character between tags, why are saved in QVector new line("\n") and spceses(" ")?

        M K 2 Replies Last reply 12 Feb 2017, 09:10
        0
        • B Bogdan112
          12 Feb 2017, 06:23

          But if i receive only the character between tags, why are saved in QVector new line("\n") and spceses(" ")?

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 12 Feb 2017, 09:10 last edited by mrjj 2 Dec 2017, 09:11
          #4

          @Bogdan112

          Hi
          I think its due to the use of
          QXmlStreamReader::Characters.

          If you do something like

          void readXML( ) {
          
          
            QString fileName = "e:/test.xml";
          
            QFile file (fileName);
            if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
              QMessageBox::critical(0, "QXSRExample::ReadXMLFile", "Couldn't open xml file", QMessageBox::Ok);
              return;
            }
          
            /* QXmlStreamReader takes any QIODevice. */
            QXmlStreamReader xml(&file);
            /* We'll parse the XML until we reach end of it.*/
            while(!xml.atEnd() && !xml.hasError()) {
              /* Read next element.*/
              QXmlStreamReader::TokenType token = xml.readNext();
              /* If token is just StartDocument, we'll go to next.*/
              if(token == QXmlStreamReader::StartDocument)
                continue;
          
              /* If token is StartElement, we'll see if we can read it.*/
              if(token == QXmlStreamReader::StartElement) {
               qDebug() << "#" << xml.name();
              }
            }
            /* Error handling. */
            if(xml.hasError())
              QMessageBox::critical(0, "parseXML", xml.errorString(), QMessageBox::Ok);
          
            //resets its internal state to the initial state.
            xml.clear();
          }
          

          Then it shows

          # "test"
          # "Page1"
          # "name1"
          # "name2"
          # "name3"
          # "name4"
          # "Page2"
          # "Val1"
          # "Val2"
          # "Val3"
          # "Val4"
          # "Page3"
          # "Val1"
          # "Val2"
          # "Val3"
          # "Val4"
          # "Page4"
          # "Val1"
          # "Val2"
          # "Val3"
          # "Val4"
          # "Page5"
          # "Val1"
          # "Val2"
          # "Val3"
          # "Val4"
          

          http://qt.shoutwiki.com/wiki/QXmlStreamReader_to_parse_XML_in_Qt

          B 1 Reply Last reply 14 Feb 2017, 13:35
          1
          • B Bogdan112
            12 Feb 2017, 06:23

            But if i receive only the character between tags, why are saved in QVector new line("\n") and spceses(" ")?

            K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 12 Feb 2017, 13:23 last edited by
            #5

            @Bogdan112 said in Parse an Xml file with Qt:

            But if i receive only the character between tags, why are saved in QVector new line("\n") and spceses(" ")?

            That's my point - there are whitespaces between the tags (not inside them). E.g.

            <Page1>\n        <name1>
            

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            3
            • M mrjj
              12 Feb 2017, 09:10

              @Bogdan112

              Hi
              I think its due to the use of
              QXmlStreamReader::Characters.

              If you do something like

              void readXML( ) {
              
              
                QString fileName = "e:/test.xml";
              
                QFile file (fileName);
                if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
                  QMessageBox::critical(0, "QXSRExample::ReadXMLFile", "Couldn't open xml file", QMessageBox::Ok);
                  return;
                }
              
                /* QXmlStreamReader takes any QIODevice. */
                QXmlStreamReader xml(&file);
                /* We'll parse the XML until we reach end of it.*/
                while(!xml.atEnd() && !xml.hasError()) {
                  /* Read next element.*/
                  QXmlStreamReader::TokenType token = xml.readNext();
                  /* If token is just StartDocument, we'll go to next.*/
                  if(token == QXmlStreamReader::StartDocument)
                    continue;
              
                  /* If token is StartElement, we'll see if we can read it.*/
                  if(token == QXmlStreamReader::StartElement) {
                   qDebug() << "#" << xml.name();
                  }
                }
                /* Error handling. */
                if(xml.hasError())
                  QMessageBox::critical(0, "parseXML", xml.errorString(), QMessageBox::Ok);
              
                //resets its internal state to the initial state.
                xml.clear();
              }
              

              Then it shows

              # "test"
              # "Page1"
              # "name1"
              # "name2"
              # "name3"
              # "name4"
              # "Page2"
              # "Val1"
              # "Val2"
              # "Val3"
              # "Val4"
              # "Page3"
              # "Val1"
              # "Val2"
              # "Val3"
              # "Val4"
              # "Page4"
              # "Val1"
              # "Val2"
              # "Val3"
              # "Val4"
              # "Page5"
              # "Val1"
              # "Val2"
              # "Val3"
              # "Val4"
              

              http://qt.shoutwiki.com/wiki/QXmlStreamReader_to_parse_XML_in_Qt

              B Offline
              B Offline
              Bogdan112
              wrote on 14 Feb 2017, 13:35 last edited by
              #6

              @mrjj said in Parse an Xml file with Qt:

              @Bogdan112

              Hi
              I think its due to the use of
              QXmlStreamReader::Characters.

              If you do something like

              void readXML( ) {
              
              
                QString fileName = "e:/test.xml";
              
                QFile file (fileName);
                if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
                  QMessageBox::critical(0, "QXSRExample::ReadXMLFile", "Couldn't open xml file", QMessageBox::Ok);
                  return;
                }
              
                /* QXmlStreamReader takes any QIODevice. */
                QXmlStreamReader xml(&file);
                /* We'll parse the XML until we reach end of it.*/
                while(!xml.atEnd() && !xml.hasError()) {
                  /* Read next element.*/
                  QXmlStreamReader::TokenType token = xml.readNext();
                  /* If token is just StartDocument, we'll go to next.*/
                  if(token == QXmlStreamReader::StartDocument)
                    continue;
              
                  /* If token is StartElement, we'll see if we can read it.*/
                  if(token == QXmlStreamReader::StartElement) {
                   qDebug() << "#" << xml.name();
                  }
                }
                /* Error handling. */
                if(xml.hasError())
                  QMessageBox::critical(0, "parseXML", xml.errorString(), QMessageBox::Ok);
              
                //resets its internal state to the initial state.
                xml.clear();
              }
              

              Then it shows

              # "test"
              # "Page1"
              # "name1"
              # "name2"
              # "name3"
              # "name4"
              # "Page2"
              # "Val1"
              # "Val2"
              # "Val3"
              # "Val4"
              # "Page3"
              # "Val1"
              # "Val2"
              # "Val3"
              # "Val4"
              # "Page4"
              # "Val1"
              # "Val2"
              # "Val3"
              # "Val4"
              # "Page5"
              # "Val1"
              # "Val2"
              # "Val3"
              # "Val4"
              

              http://qt.shoutwiki.com/wiki/QXmlStreamReader_to_parse_XML_in_Qt

              Thank you. That solved my problem.

              1 Reply Last reply
              1

              1/6

              11 Feb 2017, 15:34

              • Login

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