Parse an Xml file with Qt



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

  • Qt Champions 2016

    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.



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


  • Qt Champions 2016

    @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


  • Qt Champions 2016

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


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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.