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. Xml parsing using QXmlStreamReader

Xml parsing using QXmlStreamReader

Scheduled Pinned Locked Moved General and Desktop
qxmlstreamreadexmlpremature end o
2 Posts 2 Posters 2.7k 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.
  • T Offline
    T Offline
    Thom0801
    wrote on 28 Sept 2015, 14:27 last edited by
    #1

    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-->
    <root>
        <childA>text1</childA>
        <childA>text2</childA>
        <childB>text3</childB>
        <childA>text4</childA>
    </root>
    

    And here an example code :

    QFile file("param.xml");
    if(!file.open(QFile::ReadOnly | QFile::Text)){
      qDebug() << "Cannot read file" << file.errorString();
    }
    
    QXmlStreamReader reader(&file);
    
    if (reader.readNextStartElement()) {
      if (reader.name() == "root"){
          while(reader.readNextStartElement()){
              if(reader.name() == "childA"){
                  QString s = reader.readElementText();
                  qDebug(qPrintable(s));
              }
              else
                  reader.skipCurrentElement();
          }
      }
      else
          reader.raiseError(QObject::tr("Incorrect file"));
    }
    else{
        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?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 28 Sept 2015, 20:51 last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      2/2

      28 Sept 2015, 20:51

      • Login

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