[Solved]XML readproblems
-
Sorry here are som of the xml file
@
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<version>0.2</version>
<mains>
<main>
<main_id>2</main_id><en_main> <![CDATA[ Automotive]]>
</en_main>
<en_class_title>
<![CDATA[ Make]]>
</en_class_title>- <sections>
- <section>
<section_id>1</section_id> - <en_section>
@
etc..
etc..----[EDIT: fixed code formatting, Volker]
-
First of all in the XML example you're giving there is no element which name is "en_class".
Then, what if you add these lines after you loop :if ( s.hasError() ) { qDebug() << s.errorString(); qDebug() << "at line " << s.lineNumber() << ", column " << s.columnNumber(); qDebug() << "Error type " << s.error(); }
-
thank you
there are elements but they are further down in the xml fileHere is the result after your rows
"Premature end of document."
at line 1 , column 0
Error type 4I will search this error but if you have an answer to it I will be happy
-
Look at this: it seems that your input is not complete when you are trying to parse the XML document.
-
Thanks
I'll Try this tomorrow.I thought this would be enough
@QXmlStreamReader s(&in);@ -
Well, it depends what is you in object. If it's a file on disk, I agree with you. However if you're downloading the file for instance with QNetworkAccessManager / QNetworkReply, you better wait till the file is completely downloaded and written.
I'm pretty sure there's an example of this somewhere...
Edit : found what I wanted, when I was trying to do such a thing this thread and this one as well helped me.
-
I got the file in the same folder as the application on the disk but I will try with another xml file
-
This is the problem
@s.readElementText();@
anyone
pls -
The readElementText() method uses readElementText( ErrorOnUnexpectedElement ).
As far as I understand this error means that your file (assuming the reading of the full file is OK) doesn't contain valid XML: it tries to find the corresponding EndElement till the end of the file but cannot...Why don't you start trying to read a simple xml, like this one?
-
I did that with a simple xml file and I tried your exampel to but same error.
This is a big problem now. -
It means that you're using the readElementText in a wrong way... I'm afraid I cannot help you much more than I already tried. I'm not a big QXmlStreamReader user.
-
thank you for your help.
Is it easier to use sax or dom to get the values in the xml -
Got it to work
@void MainWindow::init()
{//QFile file("C:/testprojekt/filesXML/cache.xml"); //QFile file("C:/testprojekt/filesXML/parts.xml"); QFile file("C:/testprojekt/filesXML/simple.xml"); if (file.exists()) qDebug() << file.fileName(); else qDebug() << "NO " + file.fileName(); file.open(QIODevice::ReadOnly); QXmlStreamReader xml_stream(&file); while (!xml_stream.atEnd() && !xml_stream.hasError()) { if (xml_stream.isStartElement() && xml_stream.name() =="description") ui->textBrowser->append( + "<" + xml_stream.name().toString() + ">" + xml_stream.readElementText()); xml_stream.readNext(); } ui->textBrowser->append("-------------------------"); xml_stream.clear(); file.close();
}@