Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Using QDomDocument to parse XML

From Wiki
Jump to: navigation, search
Article Metadata
Code ExampleCompatibility
Created: symbianyucca (14 Jun 2010)
Last edited: hamishwillee (11 Oct 2012)
Featured Article
24 Oct



This code example shows you how to parse well-formed XML with QDomDocument. QDomDocument is easy and strait forward way on parsing well known XML documents.


  • Download and install the Qt SDK

QDomDocument parsing Example

XML file

<?xml version="1.0" encoding="UTF-8" ?>
<person id="1">
<person id="2">
<person id="3">

Header file

class QXSRExample : public QMainWindow
QXSRExample(QWidget *parent = 0);
private slots:
void parseXML();
QPointer<QVBoxLayout> _layout;
void setupUI();
void addPersonsToUI(QList< QMap<QString,QString> >& persons);

Source file

void QXSRExample::parseXML() {
/* We'll parse the example.xml */
QFile* file = new QFile("C://example.xml");
/* If we can't open it, let's show an error message. */
if (!file->open(QIODevice::ReadOnly | QIODevice::Text)) {
"Couldn't open example.xml",
QList< QMap<QString,QString> > persons;
/* QDomDocument takes any QIODevice. as well as QString buffer*/
QDomDocument doc("mydocument");
if (!doc.setContent(file))
//Get the root element
QDomElement docElem = doc.documentElement();
// you could check the root tag name here if it matters
QString rootTag = docElem.tagName(); // == persons
// get the node's interested in, this time only caring about person's
QDomNodeList nodeList = docElem.elementsByTagName("person");
//Check each node one by one.
for(int ii = 0;ii < nodeList.count(); ii++)
QMap<QString, QString> person;
// get the current one as QDomElement
QDomElement el =;
person["id"] = el.attribute("id"); // get and set the attribute ID
//get all data for the element, by looping through all child elements
QDomNode pEntries = el.firstChild();
while(!pEntries.isNull()) {
QDomElement peData = pEntries.toElement();
QString tagNam = peData.tagName();
if(tagNam == "firstname") { /* We've found first name. */
person["firstname"] = peData.text();
}else if(tagNam == "surname") { /* We've found surname. */
person["surname"] = peData.text();
}else if(tagNam == "email") { /* We've found email. */
person["email"] = peData.text();
}else if(tagNam == "website") { /* We've found website. */
person["website"] = peData.text();
pEntries = pEntries.nextSibling();


You are able to parse the XML and display it in the UI. QXSRExample.png

Supplementary material

  • You can test QDomDocument with a test application. The application is available for download at

See also

QXmlStreamReader to parse XML in Qt

This page was last modified on 11 October 2012, at 01:18.
1665 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.