
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
TheQXmlStreamWriter class provides an XML writer with a simple streaming API.More...
| Header: | #include <QXmlStreamWriter> |
| Since: | Qt 4.3 |
Note: All functions in this class arereentrant.
| QXmlStreamWriter() | |
| QXmlStreamWriter(QIODevice * device) | |
| QXmlStreamWriter(QByteArray * array) | |
| QXmlStreamWriter(QString * string) | |
| ~QXmlStreamWriter() | |
| bool | autoFormatting() const |
| int | autoFormattingIndent() const |
| QTextCodec * | codec() const |
| QIODevice * | device() const |
| bool | hasError() const |
| void | setAutoFormatting(bool enable) |
| void | setAutoFormattingIndent(int spacesOrTabs) |
| void | setCodec(QTextCodec * codec) |
| void | setCodec(const char * codecName) |
| void | setDevice(QIODevice * device) |
| void | writeAttribute(const QString & namespaceUri, const QString & name, const QString & value) |
| void | writeAttribute(const QString & qualifiedName, const QString & value) |
| void | writeAttribute(const QXmlStreamAttribute & attribute) |
| void | writeAttributes(const QXmlStreamAttributes & attributes) |
| void | writeCDATA(const QString & text) |
| void | writeCharacters(const QString & text) |
| void | writeComment(const QString & text) |
| void | writeCurrentToken(const QXmlStreamReader & reader) |
| void | writeDTD(const QString & dtd) |
| void | writeDefaultNamespace(const QString & namespaceUri) |
| void | writeEmptyElement(const QString & namespaceUri, const QString & name) |
| void | writeEmptyElement(const QString & qualifiedName) |
| void | writeEndDocument() |
| void | writeEndElement() |
| void | writeEntityReference(const QString & name) |
| void | writeNamespace(const QString & namespaceUri, const QString & prefix = QString()) |
| void | writeProcessingInstruction(const QString & target, const QString & data = QString()) |
| void | writeStartDocument(const QString & version) |
| void | writeStartDocument(const QString & version, bool standalone) |
| void | writeStartDocument() |
| void | writeStartElement(const QString & namespaceUri, const QString & name) |
| void | writeStartElement(const QString & qualifiedName) |
| void | writeTextElement(const QString & namespaceUri, const QString & name, const QString & text) |
| void | writeTextElement(const QString & qualifiedName, const QString & text) |
TheQXmlStreamWriter class provides an XML writer with a simple streaming API.
QXmlStreamWriter is the counterpart toQXmlStreamReader for writing XML. Like its related class, it operates on aQIODevice specified withsetDevice(). 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 withwriteStartDocument() and end it withwriteEndDocument(). This will implicitly close all remaining open tags.
Element tags are opened withwriteStartElement() followed bywriteAttribute() orwriteAttributes(), element content, and thenwriteEndElement(). A shorter formwriteEmptyElement() can be used to write empty elements, followed bywriteAttributes().
Element content consists of either characters, entity references or nested elements. It is written withwriteCharacters(), which also takes care of escaping all forbidden characters and character sequences,writeEntityReference(), or subsequent calls towriteStartElement(). A convenience methodwriteTextElement() 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.nokia.com/"); stream.writeTextElement("title","Qt Home"); stream.writeEndElement();// bookmark ... stream.writeEndDocument();
QXmlStreamWriter takes care of prefixing namespaces, all you have to do is specify thenamespaceUri 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 eitherwriteNamespace() orwriteDefaultNamespace(). Alternatively, you can bypass the stream writer's namespace support and use overloaded methods that take a qualified name instead. The namespacehttp://www.w3.org/XML/1998/namespace is implicit and mapped to the prefixxml.
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 theautoFormatting property, and customized with theautoFormattingIndent property.
Other functions arewriteCDATA(),writeComment(),writeProcessingInstruction(), andwriteDTD(). Chaining of XML streams is supported withwriteCurrentToken().
By default,QXmlStreamWriter encodes XML in UTF-8. Different encodings can be enforced usingsetCodec().
If an error occurs while writing to the underlying device,hasError() starts returning true and subsequent writes are ignored.
TheQXmlStream Bookmarks Example illustrates how to use a stream writer to write an XML bookmark file (XBEL) that was previously read in by aQXmlStreamReader.
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 theautoFormattingIndent property.
By default, auto-formatting is disabled.
This property was introduced in Qt 4.4.
Access functions:
| bool | autoFormatting() const |
| void | setAutoFormatting(bool enable) |
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 alsoautoFormatting.
Constructs a stream writer.
See alsosetDevice().
Constructs a stream writer that writes intodevice;
Constructs a stream writer that writes intoarray. This is the same as creating an xml writer that operates on aQBuffer device which in turn operates onarray.
Constructs a stream writer that writes intostring.
Destructor.
Returns the codec that is currently assigned to the stream.
See alsosetCodec().
Returns the current device associated with theQXmlStreamWriter, or 0 if no device has been assigned.
See alsosetDevice().
Returns true if the stream failed to write to the underlying device; otherwise returns false.
The error status is never reset. Writes happening after the error occurred are ignored, even if the error condition is cleared.
This function was introduced in Qt 4.8.
Sets the codec for this stream tocodec. The codec is used for encoding any data that is written. By default,QXmlStreamWriter uses UTF-8.
The encoding information is stored in the initial xml tag which gets written when you callwriteStartDocument(). Call this function before callingwriteStartDocument().
See alsocodec().
Sets the codec for this stream to theQTextCodec for the encoding specified bycodecName. Common values forcodecName include "ISO 8859-1", "UTF-8", and "UTF-16". If the encoding isn't recognized, nothing happens.
See alsoQTextCodec::codecForName().
Sets the current device todevice. If you want the stream to write into aQByteArray, you can create aQBuffer device.
See alsodevice().
Writes an attribute withname andvalue, prefixed for the specifiednamespaceUri. If the namespace has not been declared yet,QXmlStreamWriter will generate a namespace declaration for it.
This function can only be called afterwriteStartElement() before any content is written, or afterwriteEmptyElement().
This is an overloaded function.
Writes an attribute withqualifiedName andvalue.
This function can only be called afterwriteStartElement() before any content is written, or afterwriteEmptyElement().
This is an overloaded function.
Writes theattribute.
This function can only be called afterwriteStartElement() before any content is written, or afterwriteEmptyElement().
Writes the attribute vectorattributes. If a namespace referenced in an attribute not been declared yet,QXmlStreamWriter will generate a namespace declaration for it.
This function can only be called afterwriteStartElement() before any content is written, or afterwriteEmptyElement().
See alsowriteAttribute() andwriteNamespace().
Writestext as CDATA section. Iftext contains the forbidden character sequence "]]>", it is split into different CDATA sections.
This function mainly exists for completeness. Normally you should not need use it, becausewriteCharacters() automatically escapes all non-content characters.
Writestext. The characters "<", "&", and """ are escaped as entity references "<", "&, and """. To avoid the forbidden sequence "]]>", ">" is also escaped as ">".
See alsowriteEntityReference().
Writestext as XML comment, wheretext must not contain the forbidden sequence "--" or end with "-". Note that XML does not provide any way to escape "-" in a comment.
Writes the current state of thereader. All possible valid states are supported.
The purpose of this function is to support chained processing of XML data.
See alsoQXmlStreamReader::tokenType().
Writes a DTD section. Thedtd represents the entire doctypedecl production from the XML 1.0 specification.
Writes a default namespace declaration fornamespaceUri.
IfwriteStartElement() orwriteEmptyElement() was called, the declaration applies to the current element; otherwise it applies to the next child element.
Note that the namespaceshttp://www.w3.org/XML/1998/namespace (bound toxmlns) andhttp://www.w3.org/2000/xmlns/ (bound toxml) by definition cannot be declared as default.
Writes an empty element withname, prefixed for the specifiednamespaceUri. If the namespace has not been declared,QXmlStreamWriter will generate a namespace declaration for it. Subsequent calls towriteAttribute() will add attributes to this element.
See alsowriteNamespace().
This is an overloaded function.
Writes an empty element with qualified namequalifiedName. Subsequent calls towriteAttribute() will add attributes to this element.
Closes all remaining open start elements and writes a newline.
See alsowriteStartDocument().
Closes the previous start element.
See alsowriteStartElement().
Writes the entity referencename to the stream, as "&name;".
Writes a namespace declaration fornamespaceUri withprefix. Ifprefix is empty,QXmlStreamWriter assigns a unique prefix consisting of the letter 'n' followed by a number.
IfwriteStartElement() orwriteEmptyElement() was called, the declaration applies to the current element; otherwise it applies to the next child element.
Note that the prefixxml is both predefined and reserved forhttp://www.w3.org/XML/1998/namespace, which in turn cannot be bound to any other prefix. The prefixxmlns and its URIhttp://www.w3.org/2000/xmlns/ are used for the namespace mechanism itself and thus completely forbidden in declarations.
Writes an XML processing instruction withtarget anddata, wheredata must not contain the sequence "?>".
Writes a document start with the XML version numberversion.
See alsowriteEndDocument().
Writes a document start with the XML version numberversion and a standalone attributestandalone.
This function was introduced in Qt 4.5.
See alsowriteEndDocument().
This is an overloaded function.
Writes a document start with XML version number "1.0". This also writes the encoding information.
This function was introduced in Qt 4.5.
See alsowriteEndDocument() andsetCodec().
Writes a start element withname, prefixed for the specifiednamespaceUri. If the namespace has not been declared yet,QXmlStreamWriter will generate a namespace declaration for it. Subsequent calls towriteAttribute() will add attributes to this element.
See alsowriteNamespace(),writeEndElement(), andwriteEmptyElement().
This is an overloaded function.
Writes a start element withqualifiedName. Subsequent calls towriteAttribute() will add attributes to this element.
See alsowriteEndElement() andwriteEmptyElement().
Writes a text element withname, prefixed for the specifiednamespaceUri, andtext. If the namespace has not been declared,QXmlStreamWriter will generate a namespace declaration for it.
This is a convenience function equivalent to:
writeStartElement(namespaceUri, name);writeCharacters(text);writeEndElement();
This is an overloaded function.
Writes a text element withqualifiedName andtext.
This is a convenience function equivalent to:
writeStartElement(qualifiedName);writeCharacters(text);writeEndElement();
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.