While trying to traverse through an xml file, I get the error as " Premature End of Document".
My code is as follows.
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.