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. Blank out put when i am trying to parse the xml file using QXmlStreamReader Class
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 3 Posters 4.3k Views
  • 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
    BinuJanardhanan
    wrote on last edited by
    #1

    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-worxR 1 Reply Last reply
    0
    • B BinuJanardhanan

      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-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @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
      1
      • raven-worxR raven-worx

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

        B Offline
        B Offline
        BinuJanardhanan
        wrote on last edited by
        #3

        @raven-worx Yes but its showing same.

        raven-worxR 1 Reply Last reply
        0
        • B BinuJanardhanan

          @raven-worx Yes but its showing same.

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

          @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
          0
          • raven-worxR 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.

            B Offline
            B Offline
            BinuJanardhanan
            wrote on last edited by
            #5

            @raven-worx Could you explain with an example?

            raven-worxR 1 Reply Last reply
            0
            • B BinuJanardhanan

              @raven-worx Could you explain with an example?

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              @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-worxR 2 Replies Last reply
              0
              • raven-worxR raven-worx

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

                B Offline
                B Offline
                BinuJanardhanan
                wrote on last edited by
                #7

                @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
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  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
                  0
                  • raven-worxR raven-worx

                    @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-worxR Offline
                    raven-worxR Offline
                    raven-worx
                    Moderators
                    wrote on last edited by
                    #9

                    @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
                    2
                    • VRoninV VRonin

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

                      B Offline
                      B Offline
                      BinuJanardhanan
                      wrote on last edited by
                      #10

                      @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
                      0
                      • VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on last edited by
                        #11

                        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
                        2
                        • VRoninV VRonin

                          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;
                          }
                          }
                          
                          B Offline
                          B Offline
                          BinuJanardhanan
                          wrote on last edited by
                          #12

                          @VRonin Thank you .Thank you very much.

                          1 Reply Last reply
                          0

                          • Login

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