Qt Forum

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

    Forum Updated on Feb 6th

    Solved qxmlstreamreader how to skip /end

    General and Desktop
    3
    6
    1035
    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.
    • Z
      zicx last edited by

      I have a simple xml

      <?xml version="1.0"?>
      <config_file>
      
      <config symbol="A">
      	<val_1>3200</val_1>
      	<val_2>2000</val_2>
      	<val_3>1000</val_3>
      </config>
      
      </config_file>
      

      and a reader

      void XmlReader::Read() {
      
          QFile xml_file(this->filename_);
          xml_file.open( QIODevice::ReadOnly );
          this->xml_.setDevice( &xml_file );
      
          if ( xml_.readNextStartElement() && xml_.name() == "config_file") {
      
              while ( !xml_.atEnd() ) {
      
                  std::string field = xml_.name().toString().toStdString();
                  xml_.readNext();
      
                  std::string value = xml_.text().toString().toStdString();
      
                  std::cout << " Next " << field << " " << value << std::endl;
      
      
                  xml_.readNextStartElement();
      
              }
          }
      }
      

      The loop prints

      File Found
      Next config_file

      Next config

      Next val_1 3200
      Next val_1

      Next val_2 2000
      Next val_2

      Next val_3 1000
      Next val_3

      Next config

      Next config_file

      So, it seem readNextStartElement() goes to the closing elements as well ( the </...> )

      Is there a way to avoid that? Or is there something else I am doing wrong?

      1 Reply Last reply Reply Quote 0
      • P
        Paul Colby last edited by

        Hi @zicx,

        So, it seem readNextStartElement() goes to the closing elements as well ( the </...> )

        Correct. As per the docs for QXmlStreamReader::readNextStartElement:

        Reads until the next start element within the current element.

        (emphasis mine). And also:

        The current element is the element matching the most recently parsed start element of which a matching end element has not yet been reached.

        Is there a way to avoid that?

        Depending on what you want, either check the result of readNextStartElement (returns a boolean). Or, use QXmlStreamReader::skipCurrentElement end of each loop, before calling readNextStartElement.

        Cheers.

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

          while (!xml_.atEnd() && !xml_.hasError()) {
                      xml_.readNext();
          if (xml_.isStartElement()) {
          if (xml_.name() == QStringLiteral("val_1")) {
          std::cout << "Val1 " << qPrintable(xml_.readElementText())
          }
          if (xml_.name() == QStringLiteral("val_2")) {
          std::cout << "Val2 " << qPrintable(xml_.readElementText())
          }
          if (xml_.name() == QStringLiteral("val_3")) {
          std::cout << "Val3 " << qPrintable(xml_.readElementText())
          }
          }
          

          "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

          1 Reply Last reply Reply Quote 1
          • Z
            zicx last edited by

            @Paul

            "Reads until the next start element within the current element"

            Oh, I misunderstood. I expected it to jump to the next startelement... so it first reads, then I move next, then it reads again and the output was blank because there was no next in that element anymore?

            @VRonin
            The readElementext is quite neat, I like that thanks

            One more question out of curiosity. How do I read out

             <config symbol="A">
            

            the symbol code. I trued with readElementText, but that does not work

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

              if (xml_.name() == QStringLiteral("config ")) {
              std::cout << "Val1 " << qPrintable(xml_.attributes().value(QStringLiteral("symbol")))
              }
              

              "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

              Z 1 Reply Last reply Reply Quote 1
              • Z
                zicx @VRonin last edited by

                @VRonin

                thanks

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