:: printable version ::

XML writer class

Description

The Qt XML Module allows to easily read and parse XML documents, but it does not allow writing of XML. This class helps you create XML files with ease.

Changelog

  • 2007-02-15 GCC build issues solved.
  • 2007-01-28 Minor API changes.
  • 2006-06-09 Minor changes (documentation and binary compatibility).
  • 2005-09-30 First public release.

Requirements

  • Qt 4.0.x (QtCore module)

Features

  • User-defined text encoding.
  • Automatic and customizable indentation.
  • Automatic replacement of illegal XML characters.

Missing features and restrictions

  • Write errors are ignored.
::top::

Usage example

Here is a little XML document example:

<?xml version="1.0" encoding="UTF-8"?> <sample_document version="1.0"> <info> <visibility hidden="true"/> <user id="504" type="default"/> <data>Here comes some interesting text</data> </info> <info> <visibility hidden="false"/> <user id="550" type="default"/> <data>Some more text</data> </info> </sample_document>

And here is the code to write this XML snippet. It seems too long, right? It's just a trivial example; bear that you will call the XML writer from some sort of loop or method most of the time :)

#include <QFile> #include <QTextCodec> #include <QHash> #include "xmlwriter.h"
// ...
QFile file("myFile.xml"); if (!file.open(QIODevice::WriteOnly)) {
// ERROR
}
// Default text encoding: UTF-8
XmlWriter xout(&file);
// A hash is used to add attributes
QHash<QString,QString> attrs; attrs.insert("version", "1.0"); xout.writeOpenTag("sample_document", attrs); attrs.clear(); xout.writeOpenTag("info"); attrs.insert("hidden", "true"); xout.writeAtomTag("visibility", attrs); attrs.clear(); attrs.insert("id", "540"); attrs.insert("type", "default"); xout.writeAtomTag("user", attrs); attrs.clear(); xout.writeTaggedString("data", "Here comes some interesting text"); xout.writeCloseTag("info"); xout.writeOpenTag("info"); attrs.insert("hidden", "false"); xout.writeAtomTag("visibility", attrs); attrs.clear(); attrs.insert("id", "550"); attrs.insert("type", "default"); xout.writeAtomTag("user", attrs); attrs.clear(); xout.writeTaggedString("data", "Some more text"); xout.writeCloseTag("info"); xout.writeCloseTag("sample_document");
:: index :: top ::