|  | Home · Overviews · Examples

# QXmlStreamWriter Class Reference
[com.trolltech.qt.xml module]

The `QXmlStreamWriter` class provides an XML writer with a simple streaming API. More...

## Detailed Description

The `QXmlStreamWriter` class provides an XML writer with a simple streaming API.

`QXmlStreamWriter` is the pendent to `QXmlStreamReader` for writing XML. Like its related class, it operates on a `QIODevice` specified with `setDevice`. The API is simple and straight forward: For every XML token or event you want to write, the writer provides a specialized function.

You start a document with `writeStartDocument` and end it with `writeEndDocument`. This will implicitly close all remaining open tags.

Element tags are opened with `writeStartElement` followed by `writeAttribute` or `writeAttributes`, element content, and then `writeEndElement`. A shorter form `writeEmptyElement` can be used to write empty elements.

Element content consists of either characters, entity references or nested elements. It is written with `writeCharacters`, which also takes care of escaping all forbidden characters and character sequences, `writeEntityReference`, or subsequent calls to `writeStartElement`. A convenience method `writeTextElement` can be used for writing terminal elements that contain nothing but text.

`QXmlStreamWriter` takes care of prefixing namespaces, all you have to do is specify the `namespaceUri` when writing elements or attributes. If you must conform to certain prefixes, you can force the writer to use them by declaring the namespaces manually with either `writeNamespace` or `writeDefaultNamespace`. Alternatively, you can bypass the stream writer's namespace support and use overloaded methods that take a qualified name instead. The namespace http://www.w3.org/XML/1998/namespace is implicit and mapped to the prefix xml.

The stream writer can automatically format the generated XML data by adding line-breaks and indentation to empty sections between elements, making the XML data more readable for humans and easier to work with for most source code management systems. The feature can be turned on with the `autoFormatting` property.

Other functions are `writeCDATA`, `writeComment`, `writeProcessingInstruction`, and `writeDTD`. Chaining of XML streams is supported with `writeCurrentToken`.

By default, `QXmlStreamWriter` encodes XML in UTF-8. Different encodings can be enforced using `setCodec`.

The `QXmlStream Bookmarks Example` illustrates how to use a subclassed stream writer to write an XML bookmark file (XBEL) that was previously read in by a `QXmlStreamReader`.