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
Forum Updated to NodeBB v4.3 + New Features

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.5k Views 1 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.
  • 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