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--> <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?
-
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.