@mrjj said in Parse an Xml file with Qt:
@Bogdan112
Hi
I think its due to the use of
QXmlStreamReader::Characters.
If you do something like
void readXML( ) {
QString fileName = "e:/test.xml";
QFile file (fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::critical(0, "QXSRExample::ReadXMLFile", "Couldn't open xml file", QMessageBox::Ok);
return;
}
/* QXmlStreamReader takes any QIODevice. */
QXmlStreamReader xml(&file);
/* We'll parse the XML until we reach end of it.*/
while(!xml.atEnd() && !xml.hasError()) {
/* Read next element.*/
QXmlStreamReader::TokenType token = xml.readNext();
/* If token is just StartDocument, we'll go to next.*/
if(token == QXmlStreamReader::StartDocument)
continue;
/* If token is StartElement, we'll see if we can read it.*/
if(token == QXmlStreamReader::StartElement) {
qDebug() << "#" << xml.name();
}
}
/* Error handling. */
if(xml.hasError())
QMessageBox::critical(0, "parseXML", xml.errorString(), QMessageBox::Ok);
//resets its internal state to the initial state.
xml.clear();
}
Then it shows
# "test"
# "Page1"
# "name1"
# "name2"
# "name3"
# "name4"
# "Page2"
# "Val1"
# "Val2"
# "Val3"
# "Val4"
# "Page3"
# "Val1"
# "Val2"
# "Val3"
# "Val4"
# "Page4"
# "Val1"
# "Val2"
# "Val3"
# "Val4"
# "Page5"
# "Val1"
# "Val2"
# "Val3"
# "Val4"
http://qt.shoutwiki.com/wiki/QXmlStreamReader_to_parse_XML_in_Qt
Thank you. That solved my problem.