QXmlStreamWriter Class

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

Header: #include <QXmlStreamWriter>
qmake: QT += core
Since: Qt 4.3

This class was introduced in Qt 4.3.

Note: All functions in this class are reentrant.

Properties

Public Functions

bool autoFormatting() const
int autoFormattingIndent() const
void setAutoFormatting(bool)
void setAutoFormattingIndent(int spacesOrTabs)

Detailed Description

QXmlStreamWriter is the counterpart to QXmlStreamReader for writing XML. Like its related class, it operates on a QIODevice specified with setDevice(). The API is simple and straightforward: 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, followed by writeAttributes().

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.

The following abridged code snippet shows the basic use of the class to write formatted XML with indentation:

     QXmlStreamWriter stream(&output);
     stream.setAutoFormatting(true);
     stream.writeStartDocument();
     ...
     stream.writeStartElement("bookmark");
     stream.writeAttribute("href", "http://qt-project.org/");
     stream.writeTextElement("title", "Qt Project");
     stream.writeEndElement(); // bookmark
     ...
     stream.writeEndDocument();

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, and customized with the autoFormattingIndent 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().

If an error occurs while writing to the underlying device, hasError() starts returning true and subsequent writes are ignored.

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

Property Documentation

autoFormatting : bool

The auto-formatting flag of the stream writer

This property controls whether or not the stream writer automatically formats the generated XML data. If enabled, the writer automatically adds line-breaks and indentation to empty sections between elements (ignorable whitespace). The main purpose of auto-formatting is to split the data into several lines, and to increase readability for a human reader. The indentation depth can be controlled through the autoFormattingIndent property.

By default, auto-formatting is disabled.

This property was introduced in Qt 4.4.

Access functions:

bool autoFormatting() const
void setAutoFormatting(bool)

autoFormattingIndent : int

This property holds the number of spaces or tabs used for indentation when auto-formatting is enabled. Positive numbers indicate spaces, negative numbers tabs.

The default indentation is 4.

This property was introduced in Qt 4.4.

Access functions:

int autoFormattingIndent() const
void setAutoFormattingIndent(int spacesOrTabs)

See also autoFormatting.