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

Parse an Xml file with Qt

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 3.5k 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 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>
    
    
    kshegunovK 1 Reply Last reply
    0
    • B Bogdan112

      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>
      
      
      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #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 last edited by
        #3

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

        mrjjM kshegunovK 2 Replies Last reply
        0
        • B Bogdan112

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

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #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
          1
          • B Bogdan112

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

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on 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
            • mrjjM mrjj

              @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 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

              • Login

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