From 10:00 CET Friday 22nd November we will adjust how the server works to deal with some recently reported problems. Therefore there may be a load problem, if you experience more problems than usual trying to access the forum then please PM AndyS or any of the moderators so they can inform me.

Xml parsing using QXmlStreamReader

  • Hello,

    I'm trying to parse an xml file using QXmlStreamReader.

    Here my xml file (in my project directory:

    <?xml version="1.0" encoding="UTF-8"?>
    <!--example with no DTD-->

    And here an example code :

    QFile file("param.xml");
    if(! | QFile::Text)){
      qDebug() << "Cannot read file" << file.errorString();
    QXmlStreamReader reader(&file);
    if (reader.readNextStartElement()) {
      if ( == "root"){
              if( == "childA"){
                  QString s = reader.readElementText();
          reader.raiseError(QObject::tr("Incorrect file"));
        if(reader.hasError()) qDebug()<< reader.errorString();

    execution return error:

    "Premature end of document."

    Encoding is verified.
    Another strange thing occure when i'm trying to use another xml file with the same content on my project directory (test.xml) .
    Just change the line :

    QFile file("test.xml");

    In this case my prog refuse to open the file :

    Cannot read file "No such file or directory"
    QIODevice::read (QFile, "test.xml"): device not open
    "Premature end of document."

    Both files are included on my project, are encoded UTF-8, have the same content... juste not the same name....

    Any ideas?

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Your path to test.xml is relative so the file should be beside your executable. Currently your executable is located in a shadow build dir. So for your test you can use an absolute path or copy the file in the shadow build dir.