Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

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

    General and Desktop
    3
    12
    3643
    Loading More Posts
    • 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
      BinuJanardhanan last edited by

      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. ??

      raven-worx 1 Reply Last reply Reply Quote 0
      • raven-worx
        raven-worx Moderators @BinuJanardhanan last edited by

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

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        B 1 Reply Last reply Reply Quote 1
        • B
          BinuJanardhanan @raven-worx last edited by

          @raven-worx Yes but its showing same.

          raven-worx 1 Reply Last reply Reply Quote 0
          • raven-worx
            raven-worx Moderators @BinuJanardhanan last edited by raven-worx

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

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            B 1 Reply Last reply Reply Quote 0
            • B
              BinuJanardhanan @raven-worx last edited by

              @raven-worx Could you explain with an example?

              raven-worx 1 Reply Last reply Reply Quote 0
              • raven-worx
                raven-worx Moderators @BinuJanardhanan last edited by

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

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                B raven-worx 2 Replies Last reply Reply Quote 0
                • B
                  BinuJanardhanan @raven-worx last edited by

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

                  1 Reply Last reply Reply Quote 0
                  • VRonin
                    VRonin last edited by

                    http://www.walletfox.com/course/qxmlstreamreaderexample.php

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    B 1 Reply Last reply Reply Quote 0
                    • raven-worx
                      raven-worx Moderators @raven-worx last edited by

                      @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

                      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                      If you have a question please use the forum so others can benefit from the solution in the future

                      1 Reply Last reply Reply Quote 2
                      • B
                        BinuJanardhanan @VRonin last edited by

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

                        1 Reply Last reply Reply Quote 0
                        • VRonin
                          VRonin last edited by

                          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;
                          }
                          }
                          

                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                          ~Napoleon Bonaparte

                          On a crusade to banish setIndexWidget() from the holy land of Qt

                          B 1 Reply Last reply Reply Quote 2
                          • B
                            BinuJanardhanan @VRonin last edited by

                            @VRonin Thank you .Thank you very much.

                            1 Reply Last reply Reply Quote 0
                            • First post
                              Last post