Blank out put when i am trying to parse the xml file using QXmlStreamReader Class



  • I am trying to parse a xml file using QXmlStreamREader class.
    Below i have given the details.

    XML File

    <?xml version="1.0" encoding="UTF-8"?>
    	<root>
    		<Children>
    	   		<childA>text1</childA>
    			<childA>text2</childA>
      			<childB>text3</childB>
    		  	<childA>text4</childA>
    		<Children>
    	</root>
    

    The function for read the xml :

    void MainWindow::readfile()
    {
        QString filename="vtest.xml";
           QFile file(filename);
           if(!file.exists()){
               qDebug() << "no file exist "<<filename;
           }else{
               qDebug() << filename<<" file exists...";
           }
           if (file.open(QIODevice::ReadOnly | QIODevice::Text)){
               QXmlStreamReader xml(&file);
               while (!xml.atEnd()) {
                     xml.readNext();
                     qDebug()<<xml.readElementText();
               }
               if (xml.hasError()) {
                     // do error handling
               }       
                  
               }
           }
       file.close();
    }
    

    Output:

    0_1499254547469_92c68eef-2be7-4144-8bbe-357070f3ac94-Capture.JPG

    ....
    The above output showing that there is nothing in the xml file while i am using QXmlStreamReader class to read.

    But the same xml file i have read by using the below code :

    void MainWindow::readfile()
    {
        QString filename="E:\\PROJECTS\\MRI\\Work\\QT\\WORK\\xmlreadv1\\vtest.xml";
           QFile file(filename);
           if(!file.exists()){
               qDebug() << "Exists"<<filename;
           }else{
               qDebug() << filenam<<" Not Exists";
           }
           QString line;
           ui->lineEditFile->clear();
           if (file.open(QIODevice::ReadOnly | QIODevice::Text)){
               QTextStream stream(&file);
               while (!stream.atEnd()){
                   line = stream.readLine();
                   ui->lineEditFile->setText(ui->lineEditFile->text()+line+"\n");
                   qDebug() << "linea: "<<line;
               }
           }
       file.close();
    }
    

    then the Output clearly showing the full xml file.

    0_1499255965541_80f75ed4-b9b2-4c62-b8d3-63f9f5755dd7-lineCapture.JPG

    Then what is the issue when i am using the QXmlStreamReader class for reading the xml file. ??


  • Moderators

    @BinuJanardhanan
    did you read the docs about readElementText()?!



  • @raven-worx Yes but its showing same.


  • Moderators

    @BinuJanardhanan
    it shows what "same"?!

    First of all it says that you need to be in a StartElement state, which you are not. You are in the StartDocument state.
    Second it says it reads text betwen Start- and End-Element. The only elements in your XML example are the childA and childB elements.

    QXmlStreamReader is a parser. If you want the input read from the set QIODevice directly, like you also already did.



  • @raven-worx Could you explain with an example?


  • Moderators

    @BinuJanardhanan
    an example of what? I do not even know what you are trying to achieve?
    If you just want to output the contents of the XML file then go the (working) second approach of yours.
    If you need to parse the XML file (handle it's contents) then you need to go the QXmlStreamReader way.
    For this simply call xml.readNext() like you already do, but check the return value of it and react to it accordingly.



  • @raven-worx I am trying to parse a xml file.
    But readnext not read any token from the xml.
    How i can i solve the issue ?




  • Moderators

    @raven-worx said in Blank out put when i am trying to parse the xml file using QXmlStreamReader Class:

    For this simply call xml.readNext() like you already do, but check the return value of it and react to it accordingly.

    read again this comment of mine and see QXmlStreamReader::TokenType enum



  • @VRonin Thanks for your reply.
    I have tried the first example. But nothing is showing in the output.
    XML is given below

    <?xml version="1.0" encoding="UTF-8"?>
    
    <root>
        <childA>text1</childA>
        <childA>text2</childA>
        <childB>text3</childB>
        <childA>text4</childA>
    </root>
    

    And the code for parse the above xml is :

    QFile file("test.xml");
       if(!file.open(QFile::ReadOnly | QFile::Text)){
           qDebug() << "Cannot read file" << file.errorString();
           exit(0);
       }
    
       QXmlStreamReader reader(&file);
    
       if (reader.readNextStartElement()) {
           if (reader.name() == "root"){
               while(reader.readNextStartElement()){
                   if(reader.name() == "childA"){
                       QString s = reader.readElementText();
                       qDebug(qPrintable(s));
                   }
                   else
                       reader.skipCurrentElement();
               }
           }
           else
               reader.raiseError(QObject::tr("Incorrect file"));
       }
    

    In this code ,compiler not enter to if (reader.readNextStartElement()) . reader.readNextStartElement() always return false.
    What is the issue?
    Please help I am a beginner .



  • Try:

    QFile file("test.xml");
       if(!file.open(QFile::ReadOnly)){
           qDebug() << "Cannot read file" << file.errorString();
           exit(0);
       }
    
       QXmlStreamReader reader(&file);
    bool insideRoot = false;
    while(!reader.atEnd() && !reader.hasError()){
    switch(reader.readNext()){
    case QXmlStreamReader::StartElement:
    if(reader.name()=="root")
    insideRoot =true;
    else if(reader.name()=="childA" && insideRoot){
    qDebug() << reader.readElementText();
    }
    break;
    case QXmlStreamReader::EndElement:
    if(reader.name()=="root")
    insideRoot =false;
    break;
    }
    }
    


  • @VRonin Thank you .Thank you very much.


Log in to reply
 

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