Click here to Skip to main content
15,881,870 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
While trying to traverse through an xml file, I get the error as " Premature End of Document".
My code is as follows.
C#
void CxmlRd::parseXML()
{
    QString xmlFile= "D:/Neethu/xmlRd/sample.xml";
   QFile *xml= new QFile(xmlFile);
   QXmlStreamReader r(xml);
   if(!xml->open(QFile::ReadWrite|QIODevice::Text))
   {
       QMessageBox::critical(this,
                                       "QXSRExample::parseXML",
                                       "Cannot Open file",
                                         QMessageBox::Ok);

   }
   while(!r.atEnd())
   {
       if(r.readNextStartElement())
       {
           ui->textEdit->append("found element");
       }
       if(r.hasError())
       {
           QMessageBox::critical(this,
                                                         "QXSRExample::parseXML",
                                                         r.errorString(),
                                                         QMessageBox::Ok);
       }
   }
}


The xml I am trying to traverse is given below.
<pre lang="text"><?xml version="1.0" encoding="UTF-8" ?>
<persons>
<person id="1">
<firstname>John</firstname>
<surname>Doe</surname>
<email>john.doe@example.com</email>
<website>http://en.wikipedia.org/wiki/John_Doe</website>
</person>
<person id="2">
<firstname>Jane</firstname>
<surname>Doe</surname>
<email>jane.doe@example.com</email>
<website>http://en.wikipedia.org/wiki/John_Doe</website>
</person>
<person id="3">
<firstname>Matti</firstname>
<surname>Meikäläinen</surname>
<email>matti.meikalainen@example.com</email>
<website>http://fi.wikipedia.org/wiki/Matti_Meikäläinen</website>
</person>
</persons>



Please help me to fix this issue.
Posted
Updated 25-Dec-13 21:21pm
v2
Comments
Sergey Alexandrovich Kryukov 26-Dec-13 2:28am    
This is weird, indeed. The XML is well-formed. What I cannot see: did you specify using UTF-8 in your parser? Please check up.
—SA
Utheen 26-Dec-13 5:19am    
No I didn't add. Can you please explain how it can be done?
Sergey Alexandrovich Kryukov 26-Dec-13 12:19pm    
I never used this parser and Qt in general, so please see the parser documentation to determine if there is a way to specify encoding. (For UTF-8, it's possible to start with ASCII and then self-detect the encoding from XML prolog.)
—SA
Utheen 26-Dec-13 22:33pm    
ok. Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900