Parsing XML results
Although a lot of APIs provide both XML and JSON results, you may still find that some of them only offer one format. Besides, you might feel that parsing JSON in C++/Qt is not a pleasant process. You may remember how easy it is to parse the XML model in QML/Qt Quick. Well, let's see how to do this in C++/Qt.
To make use of an xml
module, we have to add xml
to QT in the project
file, the same way we did to network. This time, Qt has provided an XML reader class called QXmlStreamReader
to help us parse the XML documents. The first thing we need to do is to change the updateData
function in the Weather
class to let the Yahoo! Weather API return an XML result.
void Weather::updateData(const QString &woeid) { QUrl url("https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid = " + woeid + "&format=xml"); QNetworkRequest req(url); naManager->get(req); }
The changing of &format=json
to &format=xml
needs to be done here...