Development/Tutorials/QtDOM Tutorial

< Development‎ | Tutorials
Revision as of 11:44, 12 January 2007 by Dhaumann (Talk | contribs) (wikify a bit)

Jump to: navigation, search

Creating and loading XML files with QtDOM

Short introduction to XML

XML is a general structured format to store and exchange hierarchical data.

If you know HTML, you'll find XML quite similar (in fact, after some small modifications, a HTML file is a valid XML file): XML uses nested tags of the form <tagname>...</tagname> for tags with contents and <tagname/> for tags without content. Each tag can contain other tags, and the tag itself can have attributes of the form <tagname attribute=value>...</tagname>.

The name of the tags is not restricted (unlike HTML, which only defines a given set of proper HTML tags), so you can choose whatever name fits your name.

As an example, let us assume that you want to store holiday information into a file and use Qt to load or modify it. To get a feeling for how XML looks like, here is one possible format for such a holiday file: <?xml version='1.0' encoding='UTF-8'?> <holidayset country="at">

 <name>Holidays for Austria</name>
 <comment>This is an example and by far not complete.</comment>
   <name>New Year's Day</name>
   <comment>Happy new year!</comment>

</holidayset> This file defines a holiday set for Austria (notice the country="at" attribute to the holidayset tag). The holiday set, enclosed in <holidayset>...</holidayset> contains two holidays, each enclosed with <holiday>...</holiday>. Each of these holiday elements contains the settings for that holiday enclosed in appropriately named tag.

Notice that we did use the same tag <name> inside the <holidayset> and inside the <holiday> tags. Also note that we implicitly used specially formated (ISO-formatted) contents for the date tags, without yet specifying it. Also note that we used quite generic names for the tags, which might become a problem with complexer structure, when we want to use the same name for different purposes.

We will use this example throughout this tutorial. In our application, we want to store the holiday set in the following class:

class Holiday { public:

 Holiday( const QDate &d, const QString &n, 
          const QString &c = QString() ) 
     mDate(d), mName(n), mComment(c) {}
 ~Holiday() {}
 QDate date() { return mDate; }
 QString name() { return mName; }
 QString comment() { return mComment; }


 QDate mDate;
 QString mName, mComment;


class HolidaySet { public:

 HolidaySet( const QString &c ) : mCountry( c ) {}
 ~HolidaySet() {}
 void setCountry( const QString &c ) { mCountry = c; }
 void setName( const QString &n ) { mName = n; }
 void setComment( const QString &c ) { mComment = c; }
 QString country() { return mCountry; }
 QString name() { return mName; }
 QString comment() { return mComment; }
 void setHolidays( const QList<Holiday> &h ) { mHolidays = h; }
 QList<Holiday> holidays() { return mHolidays; }


 QString mCountry, mName, mComment;
 QList<Holiday> mHolidays;


As there are only so many sensible names, sooner or later you will find out that you will use the same tagname or attribute name for different cases with different meanings. That is the point where namespaces come in.

Creating a simple XML file with QtDOM

Loading a simple XML file

XML Namespaces

<?xml version='1.0' encoding='UTF-8'?> <h:holidays xmlns:h="">

 <h:holiday country="at">
   <h:name>New Year's Day</h:name>