Congratulations to our 2022 Qt Champions!

[SOLVED] QXMLStreamReader invalid token (0n1)

  • Dear All

    I am trying to parse xml files using QXMLStreamReader however Qt continues to say (displayed in the locals and expressions view in debug mode) that the tokens are invalid and stops parsing.

    This is a section of the code:
    @QFile* file = new QFile(filePath);
    QXmlStreamReader xml(file);
    while(!xml.atEnd() &&!xml.hasError()) {
    QXmlStreamReader::TokenType token = xml.readNext();@

    This a section of the anonymized XML:

    @<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
    <v1:Dos xmlns:v1="" xmlns:ns3="" xmlns:ns4="" xmlns:ns5="" xmlns:ns2="">
    <v1:Dossier Id="1234567" Versie="123">

    Could anybody suggest me what can be wrong?
    If I need to post additional code or XML data please contact me.

    Kind regards

  • Lifetime Qt Champion


    Could you post a complete XML example ?
    Also what are your Qt version and OS ?

  • Dear SGalst

    I think I found what the problem may be. This is the code which I used for iterating over the XML files:
    @QDirIterator it (directory);
    while (it.hasNext())
    qDebug()<<it.filePath()+ "/" +it.fileName();
    parseXML(it.filePath()+"/"+ it.fileName());

    However when I use QDebug to print out I get for the file name always a .

    @QWindowsNativeFileDialogBase::selectNameFilter: Invalid parameter 'Directories' not found in 'All Files (*)'.
    "E:/proeftuin/vac_77777827/../.." @

    Afterwards when I try to parse the file I get the following error:
    @"Premature end of document."
    at line 1 , column 0
    Error type 4 @

    I also tried to use QDir and entryList but the file names were still a .

    What can cause this problem?

    Kind regards and thanks

  • Lifetime Qt Champion

    The problem is that you are trying to parse the . and .. path which are respectively "current dir" and "parent dir"

  • Could you give me a hint what exactly is wrong in my usage of QDirIterator? I request the file name from the iterator and append it to the file path ... Is this wrong and causing the problem?

  • Lifetime Qt Champion

    Add a check for your file name, if it's either . or .. do not parse them.

    Also IIRC filePath() will give you the complete path including the filename.

  • This solved my problem:
    @ QDirIterator it (directory, QDir::NoDotAndDotDot | QDir::Files);

    Thanks a lot!!

Log in to reply