The Lord of the Rings J.R.R. Tolkien
QXmlStreamReader xmlReader(&xmlFile); while (!xmlReader.atEnd() && !xmlReader.hasError()) { QXmlStreamReader::TokenType token = xmlReader.readNext(); if (token == QXmlStreamReader::StartElement) { if (xmlReader.name() == "title") { QString title = xmlReader.readElementText(); // do something with title... } else if (xmlReader.name() == "author") { QString author = xmlReader.readElementText(); // do something with author... } } }
- Apple
- Banana
- Orange
QXmlStreamReader xmlReader(&xmlFile); while (!xmlReader.atEnd() && !xmlReader.hasError()) { QXmlStreamReader::TokenType token = xmlReader.readNext(); if (token == QXmlStreamReader::StartElement && xmlReader.name() == "item") { QString id = xmlReader.attributes().value("id").toString(); QString text = xmlReader.readElementText(); // do something with id and text... } }This code reads each XML token one by one and checks if it's a start element for an item element. If it is, it extracts the item's ID from its attributes and then calls readElementText to extract both the ID and the item text content together as a QString. Later on, we can use these QString variables to do something with the data we've extracted. In both examples, the QXmlStreamReader library package is being used to parse the XML data and extract the relevant information.