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.

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 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.

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.

Note: When writing the XML to a QString, the codec information is ignored and the XML header will not include any encoding information, since all QStrings are UTF-16. If you later convert the QString to an 8-bit format, you must arrange for the encoding information to be transmitted out-of-band.

void QXmlStreamWriter::setCodec(const char *codecName)

Sets the codec for this stream to the QTextCodec for the encoding specified by codecName. Common values for codecName include "ISO 8859-1", "UTF-8", and "UTF-16". If the encoding isn't recognized, nothing happens.

Note: When writing the XML to a QString, the codec information is ignored and the XML header will not include any encoding information, since all QStrings are UTF-16. If you later convert the QString to an 8-bit format, you must arrange for the encoding information to be transmitted out-of-band.

Writes an empty element with name, prefixed for the specified namespaceUri. If the namespace has not been declared, QXmlStreamWriter will generate a namespace declaration for it. Subsequent calls to writeAttribute() will add attributes to this element.

Note that the prefix xml is both predefined and reserved for http://www.w3.org/XML/1998/namespace, which in turn cannot be bound to any other prefix. The prefix xmlns and its URI http://www.w3.org/2000/xmlns/ are used for the namespace mechanism itself and thus completely forbidden in declarations.

Writes a start element with name, prefixed for the specified namespaceUri. If the namespace has not been declared yet, QXmlStreamWriter will generate a namespace declaration for it. Subsequent calls to writeAttribute() will add attributes to this element.