My parsing code is this.
void MainWindow::parseXml()
{
while (!xml.atEnd())
{
xml.readNext();
if (xml.isStartElement())
{
currentTag = xml.name().toString();
qDebug() << "Current tag"<< currentTag ;
}
else if (xml.isEndElement())
{
if (xml.name() == "response")
{
item->setText(0, titleString);
item->setText(1, linkString);
treeWidget->addTopLevelItem(item);
titleString.clear();
linkString.clear();
}
}
else if (xml.isCharacters() && !xml.isWhitespace())
{
if (currentTag == "client")
titleString += xml.attributes().value("id").toString();
else if (currentTag == "serial_number")
linkString += xml.text().toString();
}
}
if (xml.error() && xml.error() != QXmlStreamReader::PrematureEndOfDocumentError) {
qWarning() << "XML ERROR:" << xml.lineNumber() << ": " << xml.errorString();
http.abort();
}
}
void MainWindow::parseXml()
{
while (!xml.atEnd())
{
xml.readNext();
if (xml.isStartElement())
{
currentTag = xml.name().toString();
qDebug() << "Current tag"<< currentTag ;
}
else if (xml.isEndElement())
{
if (xml.name() == "response")
{
QTreeWidgetItem *item = new QTreeWidgetItem;
item->setText(0, titleString);
item->setText(1, linkString);
treeWidget->addTopLevelItem(item);
titleString.clear();
linkString.clear();
}
}
else if (xml.isCharacters() && !xml.isWhitespace())
{
if (currentTag == "client")
titleString += xml.attributes().value("id").toString();
else if (currentTag == "serial_number")
linkString += xml.text().toString();
}
}
if (xml.error() && xml.error() != QXmlStreamReader::PrematureEndOfDocumentError) {
qWarning() << "XML ERROR:" << xml.lineNumber() << ": " << xml.errorString();
http.abort();
}
}
To copy to clipboard, switch view to plain text mode
<response version="0.1">
<client id="1">
<name>API Test suite 7936</name>
<kind>MOBILE</kind>
<serial_number>a09b4d3b-08b7-4cdf-bf3c-a5a8fb27ee1f</serial_number>
<added_on>2009-06-25 12:33:49.182287</added_on>
<enabled>True</enabled>
</client>
</response>
<response version="0.1">
<client id="1">
<name>API Test suite 7936</name>
<kind>MOBILE</kind>
<serial_number>a09b4d3b-08b7-4cdf-bf3c-a5a8fb27ee1f</serial_number>
<added_on>2009-06-25 12:33:49.182287</added_on>
<enabled>True</enabled>
</client>
</response>
To copy to clipboard, switch view to plain text mode
I am able to get the serial no from the response, but to get the value of id in client tag is proving hard.
how can i get the value from a attribute and convert it into string??
Bookmarks