Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse an XML string in Qt

Tags:

xml

qt

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?

like image 969
Tirupati Balan Avatar asked Jul 15 '10 11:07

Tirupati Balan


3 Answers

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();
like image 170
metdos Avatar answered Oct 04 '22 15:10

metdos


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;
}
like image 24
Muhammad Suleman Avatar answered Oct 04 '22 17:10

Muhammad Suleman


The best way is to use Qt's XML Patterns module.

http://doc.qt.io/archives/4.6/qxmlquery.html

like image 35
guruz Avatar answered Oct 04 '22 15:10

guruz