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. Premature End of document parsing XML that includes comments

Premature End of document parsing XML that includes comments

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 5 Posters 1.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.
  • P Offline
    P Offline
    pmh4514
    wrote on 31 Mar 2021, 14:14 last edited by
    #1

    Hello,

    I'm using Qt 5.14.1 on 64bit Win10 with MSVC2017 C++
    I have a very simple XML document, verified to be well formed, and some simple code using QXmlSreamReader to parse through it using the below code. This all works just fine.

    The problem I'm finding is that if I include any commented text (like <!-- comment here -->) anywhere within the document, my error check warns of "Premature end of document"

    How can I resolve this? Must comments be written differently in the file?

        QXmlStreamReader xml(&file);
        while(!xml.atEnd() && !xml.hasError()) {
            QXmlStreamReader::TokenType token = xml.readNext();
    
            if(token == QXmlStreamReader::StartDocument) {
                continue;
            }
    
            if(token == QXmlStreamReader::StartElement && xml.name() == "modes")
            {
                xml.readNext();
    
                 while(!(xml.tokenType() == QXmlStreamReader::EndElement && xml.name() == "modes"))
                 {
                    if(xml.tokenType() == QXmlStreamReader::StartElement){
                            // do some stuff
                            xml.readNext();
                    }
                 }
         }
         if(xml.hasError()){
                // notify error
         }
    
    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 31 Mar 2021, 18:37 last edited by
      #2

      Simplify the xml until your find the problematic sequence and then create a non-parseable valid xml which you can post here.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • P Offline
        P Offline
        pmh4514
        wrote on 31 Mar 2021, 19:24 last edited by
        #3

        I've done that already.
        Everything in the XML parses fine until I add any comment inside the document. Anywhere within the document, any comment (<!-- comment ->) triggers the error

        J 1 Reply Last reply 1 Apr 2021, 08:55
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 31 Mar 2021, 19:26 last edited by
          #4

          So can you provide us a minimal compilable example and the minimal xml for it then?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • P Offline
            P Offline
            pmh4514
            wrote on 31 Mar 2021, 21:10 last edited by
            #5

            ok I'll try to put together a small sample app. thanks!

            1 Reply Last reply
            0
            • N Offline
              N Offline
              nagesh
              wrote on 31 Mar 2021, 23:39 last edited by
              #6

              @pmh4514 I feel the problem is due to code for reading xml in while loop is entering "infinite loop"

              As the code in while loop only checks for token type startElement to readNext().

              if(xml.tokenType() == QXmlStreamReader::StartElement)
                             {
                                      // do some stuff
                                      xml.readNext();
                              }
              

              What if xml encounters the QXmlStreamReader::Comment type token?
              xml reading is not advancing hence while condition always holds good if it encounters comment tag in between.

              J 1 Reply Last reply 1 Apr 2021, 05:30
              0
              • N nagesh
                31 Mar 2021, 23:39

                @pmh4514 I feel the problem is due to code for reading xml in while loop is entering "infinite loop"

                As the code in while loop only checks for token type startElement to readNext().

                if(xml.tokenType() == QXmlStreamReader::StartElement)
                               {
                                        // do some stuff
                                        xml.readNext();
                                }
                

                What if xml encounters the QXmlStreamReader::Comment type token?
                xml reading is not advancing hence while condition always holds good if it encounters comment tag in between.

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 1 Apr 2021, 05:30 last edited by
                #7

                @nagesh No, see

                while(!xml.atEnd() && !xml.hasError()) {
                        QXmlStreamReader::TokenType token = xml.readNext(); // <---
                

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  nagesh
                  wrote on 1 Apr 2021, 06:20 last edited by
                  #8

                  @jsulm I was referring to this while loop,,

                  while(!(xml.tokenType() == QXmlStreamReader::EndElement && xml.name() == "modes"))
                  
                  1 Reply Last reply
                  1
                  • P pmh4514
                    31 Mar 2021, 19:24

                    I've done that already.
                    Everything in the XML parses fine until I add any comment inside the document. Anywhere within the document, any comment (<!-- comment ->) triggers the error

                    J Offline
                    J Offline
                    JonB
                    wrote on 1 Apr 2021, 08:55 last edited by JonB 4 Jan 2021, 08:59
                    #9

                    @pmh4514 said in Premature End of document parsing XML that includes comments:

                    (<!-- comment ->)

                    If that is genuinely what you have written, -> is not the XML close-comment marker. The comment will never end, all subsequent lines will be eaten into the comment, and you will get Premature end of document error.

                    You really should check your XML document is well-formed after you have added your comment if you want to understand unexpected behaviour. As I say, with what you show it is not well-formed.

                    I believe Qt Creator does XML syntax checking for you if you put your XML into a .xml file and edit it, so it should show you that the comment never ends and the document is not well-formed?

                    1 Reply Last reply
                    3
                    • P Offline
                      P Offline
                      pmh4514
                      wrote on 1 Apr 2021, 12:31 last edited by
                      #10

                      Thanks all.

                      @JonB -the malformed comment was my unfortunate typo here on the Qt forum post only.

                      The XML is well formed. The entirety is below.

                      That said, @nagesh was on the right track. I added the following to my control loop and now everything works properly when comment blocks are included

                              if(token == QXmlStreamReader::Comment){
                                  continue;
                              }
                      

                      Full XML:

                      <?xml version="1.0"?>
                      <!-- comments here -->
                      <modes>
                          <mode>
                              <label>Normal</label>
                              <zoom_index>0</zoom_index>
                              <undersample_rate>0</undersample_rate>
                          </mode>	
                          <mode>
                              <label>High</label>
                              <zoom_index>0</zoom_index>
                              <undersample_rate>2048</undersample_rate>
                          </mode>	
                      </modes>
                      
                      1 Reply Last reply
                      1

                      3/10

                      31 Mar 2021, 19:24

                      topic:navigator.unread, 7
                      • Login

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