I am developing an application in that after making a web service I got the response from the server which is in the XML tag.
The response:
<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n
<string...... /\">Hello World</string>
I want to read only the "Hello World" string. How should I parse it?
I hope this helps:
QByteArray xmlText;
//Get your xml into xmlText(you can use QString instead og QByteArray)
QDomDocument doc;
doc.setContent(xmlText);
QDomNodeList list=doc.elementsByName("string");
QString helloWorld=list.at(0).toElement().text();
try this... !
QFile* file = new QFile(fileName);
if (!file->open(QIODevice::ReadOnly | QIODevice::Text))
{
QMessageBox::critical(this, "QXSRExample::ReadXMLFile", "Couldn't open xml file", QMessageBox::Ok);
return;
}
QXmlStreamReader xml(file);
QXmlStreamReader::TokenType token;
while(!xml.atEnd() && !xml.hasError())
{
/* Read next element.*/
token = xml.readNext();
/* If token is just StartDocument, we'll go to next.*/
if(token == QXmlStreamReader::StartDocument)
continue;
if(token == QXmlStreamReader::Characters)
QMessage::information(this,"all text", xml.text().toString());
continue;
}
The best way is to use Qt's XML Patterns module.
http://doc.qt.io/archives/4.6/qxmlquery.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With