Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Premature End of document parsing XML that includes comments



  • 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
         }
    

  • Lifetime Qt Champion

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



  • 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


  • Lifetime Qt Champion

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



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



  • @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.


  • Lifetime Qt Champion

    @nagesh No, see

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


  • @jsulm I was referring to this while loop,,

    while(!(xml.tokenType() == QXmlStreamReader::EndElement && xml.name() == "modes"))
    


  • @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?



  • 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>
    

Log in to reply