QDomDocument doc; doc.setContent(""); QDomElement root = doc.documentElement(); QDomNodeList items = root.elementsByTagName("item"); for (int i = 0; i < items.count(); i++) { QDomElement item = items.at(i).toElement(); qDebug() << "Item" << i << ":" << item.text(); } - Item 1
- Item 2
QDomDocument doc; doc.setContent("In this example, we create another QDomDocument from an XML string, find the first "person" element using elementsByTagName, and then extract the "name" and "age" elements from it by chaining elementsByTagName() calls with toElement() and text() calls. We then output the person's name and age. The QDomElement elementsByTagName function is provided by the Qt Core module, specifically the QDom module."); QDomElement root = doc.documentElement(); QDomNodeList persons = root.elementsByTagName("person"); if (persons.count() > 0) { QDomElement person = persons.at(0).toElement(); QString name = person.elementsByTagName("name").at(0).toElement().text(); int age = person.elementsByTagName("age").at(0).toElement().text().toInt(); qDebug() << "Person:" << name << "(" << age << "years old)"; } John Doe 30