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::readNext() Call Doesn't Read Next Element
Forum Updated to NodeBB v4.3 + New Features

QXmlStreamReader::readNext() Call Doesn't Read Next Element

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 699 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.
  • C Offline
    C Offline
    Crag_Hack
    wrote on last edited by
    #1

    Hi I have a peculiar and (hopefully) simple question. I have a well formed XML file created by QXmlStreamWriter. For some odd reason it takes two readNext() calls to transition from one element to the next when parsing the file with QXmlStreamReader. Any ideas what I might be doing wrong?

    Here is the relevant part of the XML file:

    <?xml version="1.0" encoding="UTF-8"?>
    <jobSettings>
        <advancedJobs>
            <job name="Simple Job">
                <simple>true</simple>
    

    And here's the code:

    if (xmlReader.name() == "job")
    {
        BackupJob job;
        job.setName(xmlReader.attributes().value("name").toString());
        xmlReader.readNext();
        xmlReader.readNext();
        QString awef = xmlReader.name().toString();
    

    If I only do one readNext() call awef is set to "". After a second call it is set to "simple". I'm flabbergasted!

    Thanks for the help.

    JonBJ 1 Reply Last reply
    0
    • C Crag_Hack

      Hi I have a peculiar and (hopefully) simple question. I have a well formed XML file created by QXmlStreamWriter. For some odd reason it takes two readNext() calls to transition from one element to the next when parsing the file with QXmlStreamReader. Any ideas what I might be doing wrong?

      Here is the relevant part of the XML file:

      <?xml version="1.0" encoding="UTF-8"?>
      <jobSettings>
          <advancedJobs>
              <job name="Simple Job">
                  <simple>true</simple>
      

      And here's the code:

      if (xmlReader.name() == "job")
      {
          BackupJob job;
          job.setName(xmlReader.attributes().value("name").toString());
          xmlReader.readNext();
          xmlReader.readNext();
          QString awef = xmlReader.name().toString();
      

      If I only do one readNext() call awef is set to "". After a second call it is set to "simple". I'm flabbergasted!

      Thanks for the help.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Crag_Hack
      Print out the QXmlStreamReader::TokenType returned by each readNext(). If it's QXmlStreamReader::Characters, check isWhitespace().

      Maybe you should look at readNextStartElement() rather than readNext().

      1 Reply Last reply
      1
      • C Offline
        C Offline
        Crag_Hack
        wrote on last edited by Crag_Hack
        #3

        Thanks JonB. The blank name readNext() call is type 6 QXmlStreamReader::Characters and true for isWhiteSpace().

        Why is readNext() reading whitespace? This isn't desirable behavior is it?

        Is the only way to bypass this issue by using readNextStartElement() or testing for whitespace and executing an additional readNext() call in the case of whitespace?

        JonBJ 1 Reply Last reply
        0
        • C Crag_Hack

          Thanks JonB. The blank name readNext() call is type 6 QXmlStreamReader::Characters and true for isWhiteSpace().

          Why is readNext() reading whitespace? This isn't desirable behavior is it?

          Is the only way to bypass this issue by using readNextStartElement() or testing for whitespace and executing an additional readNext() call in the case of whitespace?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Crag_Hack said in QXmlStreamReader::readNext() Call Doesn't Read Next Element:

          The blank name readNext() call is type 6 is QXmlStreamReader::Characters and true for isWhiteSpace().

          :-)

          Why is readNext() reading whitespace? This isn't desirable behavior is it?

          Because it's an XML reader, and that is the next token. Which is not necessarily the next element!

          Is the only way to bypass this issue by using readNextStartElement() or testing for whitespace and executing an additional readNext() call in the case of whitespace?

          Indeed. But use readNextStartElement(), that's what it's there for. Normally for "high level" purposes you are only interested in reading elements. You don't care about whitespace or comments etc. This is not special to Qt, you will find the same distinction in other XML reader/writers.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Crag_Hack
            wrote on last edited by Crag_Hack
            #5

            Awesome JonB got thigs working thanks. My code was already catered to readNext() call so I replaced all readNext() calls with:

            void Program::readNextXMLElement(QXmlStreamReader &xmlReader)
            {
                xmlReader.readNext();
                while (xmlReader.isWhitespace())
                    xmlReader.readNext();
            }
            
            JonBJ 1 Reply Last reply
            0
            • C Crag_Hack

              Awesome JonB got thigs working thanks. My code was already catered to readNext() call so I replaced all readNext() calls with:

              void Program::readNextXMLElement(QXmlStreamReader &xmlReader)
              {
                  xmlReader.readNext();
                  while (xmlReader.isWhitespace())
                      xmlReader.readNext();
              }
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @Crag_Hack
              You should not need to call readNext(). You should not need to call isWhitespace(). You should only need to call readNextStartElement(). Did you try with that? Your readNextXMLElement() looks like a not-so-comprehensive readNextStartElement().

              Implementation is at https://code.woboq.org/qt6/qtbase/src/corelib/serialization/qxmlstream.cpp.html#_ZN16QXmlStreamReader20readNextStartElementEv

              bool QXmlStreamReader::readNextStartElement()
              {
                  while (readNext() != Invalid) {
                      if (isEndElement())
                          return false;
                      else if (isStartElement())
                          return true;
                  }
                  return false;
              }
              

              OIC, you wrote:

              My code was already catered to readNext() call

              So you mean what you have is designed around that, for right or for wrong. I think you should rename your readNextXMLElement(), to readNextNonWhitespace().

              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