QXmlStreamWriter Class▲
-
Header: QXmlStreamWriter
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
-
qmake: QT += core
-
Group: QXmlStreamWriter is part of xml-tools
Detailed Description▲
https:// is the counterpart to https:// for writing XML. Like its related class, it operates on a https:// specified with https://(). 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 https://() and end it with https://(). This will implicitly close all remaining open tags.
Element tags are opened with https://() followed by https://() or https://(), element content, and then https://(). A shorter form https://() can be used to write empty elements, followed by https://().
Element content consists of either characters, entity references or nested elements. It is written with https://(), which also takes care of escaping all forbidden characters and character sequences, https://(), or subsequent calls to https://(). A convenience method https://() 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();https:// 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 https://() or https://(). 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 https:// property, and customized with the https:// property.
Other functions are https://(), https://(), https://(), and https://(). Chaining of XML streams is supported with https://().
https:// always encodes XML in UTF-8.
If an error occurs while writing to the underlying device, https://() starts returning true and subsequent writes are ignored.
The https:// 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▲
This property holds 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 https:// property.
By default, auto-formatting is disabled.
Access functions:
-
bool https://() const
-
void https://(bool enable)
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.
Access functions:
-
int autoFormattingIndent() const
-
void setAutoFormattingIndent(int spacesOrTabs)
See Also▲
See also https://
Member Function Documentation▲
QXmlStreamWriter::QXmlStreamWriter()▲
[explicit] QXmlStreamWriter::QXmlStreamWriter(QIODevice *device)▲
Constructs a stream writer that writes into device;
[explicit] QXmlStreamWriter::QXmlStreamWriter(QByteArray *array)▲
Constructs a stream writer that writes into array. This is the same as creating an xml writer that operates on a https:// device which in turn operates on array.
[explicit] QXmlStreamWriter::QXmlStreamWriter(QString *string)▲
Constructs a stream writer that writes into string.
QXmlStreamWriter::~QXmlStreamWriter()▲
Destructor.
bool QXmlStreamWriter::autoFormatting() const▲
Returns true if auto formatting is enabled, otherwise false.
Getter function for property https://.
See Also▲
See also https://()
QIODevice *QXmlStreamWriter::device() const▲
Returns the current device associated with the https://, or nullptr if no device has been assigned.
See Also▲
See also https://()
bool QXmlStreamWriter::hasError() const▲
Returns true if writing failed.
This can happen if the stream failed to write to the underlying device or if the data to be written contained invalid characters.
The error status is never reset. Writes happening after the error occurred may be ignored, even if the error condition is cleared.
void QXmlStreamWriter::setAutoFormatting(bool enable)▲
Enables auto formatting if enable is true, otherwise disables it.
The default value is false.
Setter function for property https://.
See Also▲
See also https://()
void QXmlStreamWriter::setDevice(QIODevice *device)▲
Sets the current device to device. If you want the stream to write into a https://, you can create a https:// device.
See Also▲
See also https://()
void QXmlStreamWriter::writeAttribute(const QString &namespaceUri, const QString &name, const QString &value)▲
Writes an attribute with name and value, prefixed for the specified namespaceUri. If the namespace has not been declared yet, https:// will generate a namespace declaration for it.
This function can only be called after https://() before any content is written, or after https://().
void QXmlStreamWriter::writeAttribute(const QString &qualifiedName, const QString &value)▲
This is an overloaded function.
Writes an attribute with qualifiedName and value.
This function can only be called after https://() before any content is written, or after https://().
void QXmlStreamWriter::writeAttribute(const QXmlStreamAttribute &attribute)▲
This is an overloaded function.
Writes the attribute.
This function can only be called after https://() before any content is written, or after https://().
void QXmlStreamWriter::writeAttributes(const QXmlStreamAttributes &attributes)▲
Writes the attribute vector attributes. If a namespace referenced in an attribute not been declared yet, https:// will generate a namespace declaration for it.
This function can only be called after https://() before any content is written, or after https://().
See Also▲
See also https://(), https://()
void QXmlStreamWriter::writeCDATA(const QString &text)▲
Writes text as CDATA section. If text 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, because https://() automatically escapes all non-content characters.
void QXmlStreamWriter::writeCharacters(const QString &text)▲
Writes text. The characters "<", "&", and """ are escaped as entity references "<", "&, and """. To avoid the forbidden sequence "]]>", ">" is also escaped as ">".
See Also▲
See also https://()
void QXmlStreamWriter::writeComment(const QString &text)▲
Writes text as XML comment, where text must not contain the forbidden sequence "–" or end with "-". Note that XML does not provide any way to escape "-" in a comment.
void QXmlStreamWriter::writeCurrentToken(const QXmlStreamReader &reader)▲
Writes the current state of the reader. All possible valid states are supported.
The purpose of this function is to support chained processing of XML data.
See Also▲
See also https://()
void QXmlStreamWriter::writeDTD(const QString &dtd)▲
Writes a DTD section. The dtd represents the entire doctypedecl production from the XML 1.0 specification.
void QXmlStreamWriter::writeDefaultNamespace(const QString &namespaceUri)▲
Writes a default namespace declaration for namespaceUri.
If https://() or https://() was called, the declaration applies to the current element; otherwise it applies to the next child element.
Note that the namespaces http://www.w3.org/XML/1998/namespace (bound to xmlns) and http://www.w3.org/2000/xmlns/ (bound to xml) by definition cannot be declared as default.
void QXmlStreamWriter::writeEmptyElement(const QString &namespaceUri, const QString &name)▲
Writes an empty element with name, prefixed for the specified namespaceUri. If the namespace has not been declared, https:// will generate a namespace declaration for it. Subsequent calls to https://() will add attributes to this element.
See Also▲
See also https://()
void QXmlStreamWriter::writeEmptyElement(const QString &qualifiedName)▲
This is an overloaded function.
Writes an empty element with qualified name qualifiedName. Subsequent calls to https://() will add attributes to this element.
void QXmlStreamWriter::writeEndDocument()▲
void QXmlStreamWriter::writeEndElement()▲
void QXmlStreamWriter::writeEntityReference(const QString &name)▲
Writes the entity reference name to the stream, as "&name;".
void QXmlStreamWriter::writeNamespace(const QString &namespaceUri, const QString &prefix = QString())▲
Writes a namespace declaration for namespaceUri with prefix. If prefix is empty, https:// assigns a unique prefix consisting of the letter 'n' followed by a number.
If https://() or https://() was called, the declaration applies to the current element; otherwise it applies to the next child 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.
void QXmlStreamWriter::writeProcessingInstruction(const QString &target, const QString &data = QString())▲
Writes an XML processing instruction with target and data, where data must not contain the sequence "?>".
void QXmlStreamWriter::writeStartDocument(const QString &version)▲
void QXmlStreamWriter::writeStartDocument()▲
This is an overloaded function.
Writes a document start with XML version number "1.0".
See Also▲
See also https://()
void QXmlStreamWriter::writeStartDocument(const QString &version, bool standalone)▲
Writes a document start with the XML version number version and a standalone attribute standalone.
See Also▲
See also https://()
void QXmlStreamWriter::writeStartElement(const QString &namespaceUri, const QString &name)▲
Writes a start element with name, prefixed for the specified namespaceUri. If the namespace has not been declared yet, https:// will generate a namespace declaration for it. Subsequent calls to https://() will add attributes to this element.
See Also▲
See also https://(), https://(), https://()
void QXmlStreamWriter::writeStartElement(const QString &qualifiedName)▲
This is an overloaded function.
Writes a start element with qualifiedName. Subsequent calls to https://() will add attributes to this element.
See Also▲
See also https://(), https://()
void QXmlStreamWriter::writeTextElement(const QString &namespaceUri, const QString &name, const QString &text)▲
Writes a text element with name, prefixed for the specified namespaceUri, and text. If the namespace has not been declared, https:// will generate a namespace declaration for it.
This is a convenience function equivalent to:
writeStartElement(namespaceUri, name);
writeCharacters(text);
writeEndElement();void QXmlStreamWriter::writeTextElement(const QString &qualifiedName, const QString &text)▲
This is an overloaded function.
Writes a text element with qualifiedName and text.
This is a convenience function equivalent to:
writeStartElement(qualifiedName);
writeCharacters(text);
writeEndElement();

