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. qxmlstreamreader how to skip /end
Forum Updated to NodeBB v4.3 + New Features

qxmlstreamreader how to skip /end

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.3k 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.
  • Z Offline
    Z Offline
    zicx
    wrote on last edited by
    #1

    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
    0
    • Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by
      #2

      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
      1
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3
        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
        1
        • Z Offline
          Z Offline
          zicx
          wrote on last edited by
          #4

          @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
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #5
            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
            1
            • VRoninV VRonin
              if (xml_.name() == QStringLiteral("config ")) {
              std::cout << "Val1 " << qPrintable(xml_.attributes().value(QStringLiteral("symbol")))
              }
              
              Z Offline
              Z Offline
              zicx
              wrote on last edited by
              #6

              @VRonin

              thanks

              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