QXmlStreamWriter¶
The
QXmlStreamWriter
class provides an XML writer with a simple streaming API. More…
Synopsis¶
Functions¶
def
autoFormatting
()def
autoFormattingIndent
()def
codec
()def
device
()def
hasError
()def
setAutoFormatting
(arg__1)def
setAutoFormattingIndent
(spacesOrTabs)def
setCodec
(codec)def
setCodec
(codecName)def
setDevice
(device)def
writeAttribute
(attribute)def
writeAttribute
(namespaceUri, name, value)def
writeAttribute
(qualifiedName, value)def
writeAttributes
(attributes)def
writeCDATA
(text)def
writeCharacters
(text)def
writeComment
(text)def
writeCurrentToken
(reader)def
writeDTD
(dtd)def
writeDefaultNamespace
(namespaceUri)def
writeEmptyElement
(namespaceUri, name)def
writeEmptyElement
(qualifiedName)def
writeEndDocument
()def
writeEndElement
()def
writeEntityReference
(name)def
writeNamespace
(namespaceUri[, prefix=””])def
writeProcessingInstruction
(target[, data=””])def
writeStartDocument
()def
writeStartDocument
(version)def
writeStartDocument
(version, standalone)def
writeStartElement
(namespaceUri, name)def
writeStartElement
(qualifiedName)def
writeTextElement
(namespaceUri, name, text)def
writeTextElement
(qualifiedName, text)
Detailed Description¶
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 with
writeStartDocument()
and end it withwriteEndDocument()
. This will implicitly close all remaining open tags.Element tags are opened with
writeStartElement()
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 with
writeCharacters()
, 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-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 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 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 theautoFormattingIndent
property.Other functions are
writeCDATA()
,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.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
.
- class PySide2.QtCore.QXmlStreamWriter¶
PySide2.QtCore.QXmlStreamWriter(array)
PySide2.QtCore.QXmlStreamWriter(device)
- param array:
- param device:
Constructs a stream writer.
See also
Constructs a stream writer that writes into
array
. 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 into
device
;
- PySide2.QtCore.QXmlStreamWriter.autoFormatting()¶
- Return type:
bool
Returns
true
if auto formatting is enabled, otherwisefalse
.See also
- PySide2.QtCore.QXmlStreamWriter.autoFormattingIndent()¶
- Return type:
int
See also
- PySide2.QtCore.QXmlStreamWriter.codec()¶
- Return type:
Returns the codec that is currently assigned to the stream.
See also
- PySide2.QtCore.QXmlStreamWriter.device()¶
- Return type:
Returns the current device associated with the
QXmlStreamWriter
, orNone
if no device has been assigned.See also
- PySide2.QtCore.QXmlStreamWriter.hasError()¶
- Return type:
bool
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.
- PySide2.QtCore.QXmlStreamWriter.setAutoFormatting(arg__1)¶
- Parameters:
arg__1 – bool
Enables auto formatting if
enable
istrue
, otherwise disables it.The default value is
false
.See also
- PySide2.QtCore.QXmlStreamWriter.setAutoFormattingIndent(spacesOrTabs)¶
- Parameters:
spacesOrTabs – int
See also
- PySide2.QtCore.QXmlStreamWriter.setCodec(codec)¶
- Parameters:
codec –
PySide2.QtCore.QTextCodec
Sets the codec for this stream to
codec
. 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 call
writeStartDocument()
. Call this function before callingwriteStartDocument()
.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 theQString
to an 8-bit format, you must arrange for the encoding information to be transmitted out-of-band.See also
- PySide2.QtCore.QXmlStreamWriter.setCodec(codecName)
- Parameters:
codecName – str
Sets the codec for this stream to the
QTextCodec
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.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 theQString
to an 8-bit format, you must arrange for the encoding information to be transmitted out-of-band.See also
- PySide2.QtCore.QXmlStreamWriter.setDevice(device)¶
- Parameters:
device –
PySide2.QtCore.QIODevice
Sets the current device to
device
. If you want the stream to write into aQByteArray
, you can create aQBuffer
device.See also
- PySide2.QtCore.QXmlStreamWriter.writeAttribute(namespaceUri, name, value)¶
- Parameters:
namespaceUri – str
name – str
value – str
Writes an attribute with
name
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 after
writeStartElement()
before any content is written, or afterwriteEmptyElement()
.
- PySide2.QtCore.QXmlStreamWriter.writeAttribute(qualifiedName, value)
- Parameters:
qualifiedName – str
value – str
This is an overloaded function.
Writes an attribute with
qualifiedName
andvalue
.This function can only be called after
writeStartElement()
before any content is written, or afterwriteEmptyElement()
.
- PySide2.QtCore.QXmlStreamWriter.writeAttribute(attribute)
- Parameters:
attribute –
PySide2.QtCore.QXmlStreamAttribute
This is an overloaded function.
Writes the
attribute
.This function can only be called after
writeStartElement()
before any content is written, or afterwriteEmptyElement()
.
- PySide2.QtCore.QXmlStreamWriter.writeAttributes(attributes)¶
- Parameters:
attributes –
PySide2.QtCore.QXmlStreamAttributes
Writes the attribute vector
attributes
. 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 after
writeStartElement()
before any content is written, or afterwriteEmptyElement()
.See also
- PySide2.QtCore.QXmlStreamWriter.writeCDATA(text)¶
- Parameters:
text – str
Writes
text
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, because
writeCharacters()
automatically escapes all non-content characters.
- PySide2.QtCore.QXmlStreamWriter.writeCharacters(text)¶
- Parameters:
text – str
Writes
text
. The characters “<”, “&”, and “”” are escaped as entity references “<”, “&, and “"”. To avoid the forbidden sequence “]]>”, “>” is also escaped as “>”.See also
- PySide2.QtCore.QXmlStreamWriter.writeComment(text)¶
- Parameters:
text – str
Writes
text
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.
- PySide2.QtCore.QXmlStreamWriter.writeCurrentToken(reader)¶
- Parameters:
reader –
PySide2.QtCore.QXmlStreamReader
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
- PySide2.QtCore.QXmlStreamWriter.writeDTD(dtd)¶
- Parameters:
dtd – str
Writes a DTD section. The
dtd
represents the entire doctypedecl production from the XML 1.0 specification.
- PySide2.QtCore.QXmlStreamWriter.writeDefaultNamespace(namespaceUri)¶
- Parameters:
namespaceUri – str
Writes a default namespace declaration for
namespaceUri
.If
writeStartElement()
orwriteEmptyElement()
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.
- PySide2.QtCore.QXmlStreamWriter.writeEmptyElement(namespaceUri, name)¶
- Parameters:
namespaceUri – str
name – str
Writes an empty element with
name
, 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 also
- PySide2.QtCore.QXmlStreamWriter.writeEmptyElement(qualifiedName)
- Parameters:
qualifiedName – str
This is an overloaded function.
Writes an empty element with qualified name
qualifiedName
. Subsequent calls towriteAttribute()
will add attributes to this element.
- PySide2.QtCore.QXmlStreamWriter.writeEndDocument()¶
Closes all remaining open start elements and writes a newline.
See also
- PySide2.QtCore.QXmlStreamWriter.writeEndElement()¶
Closes the previous start element.
See also
- PySide2.QtCore.QXmlStreamWriter.writeEntityReference(name)¶
- Parameters:
name – str
Writes the entity reference
name
to the stream, as “&``name`` ;”.
- PySide2.QtCore.QXmlStreamWriter.writeNamespace(namespaceUri[, prefix=""])¶
- Parameters:
namespaceUri – str
prefix – str
Writes a namespace declaration for
namespaceUri
withprefix
. Ifprefix
is empty,QXmlStreamWriter
assigns a unique prefix consisting of the letter ‘n’ followed by a number.If
writeStartElement()
orwriteEmptyElement()
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.
- PySide2.QtCore.QXmlStreamWriter.writeProcessingInstruction(target[, data=""])¶
- Parameters:
target – str
data – str
Writes an XML processing instruction with
target
anddata
, wheredata
must not contain the sequence “?>”.
- PySide2.QtCore.QXmlStreamWriter.writeStartDocument()¶
This is an overloaded function.
Writes a document start with XML version number “1.0”. This also writes the encoding information.
See also
- PySide2.QtCore.QXmlStreamWriter.writeStartDocument(version)
- Parameters:
version – str
Writes a document start with the XML version number
version
.See also
- PySide2.QtCore.QXmlStreamWriter.writeStartDocument(version, standalone)
- Parameters:
version – str
standalone – bool
Writes a document start with the XML version number
version
and a standalone attributestandalone
.See also
- PySide2.QtCore.QXmlStreamWriter.writeStartElement(namespaceUri, name)¶
- Parameters:
namespaceUri – str
name – str
Writes a start element with
name
, 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.
- PySide2.QtCore.QXmlStreamWriter.writeStartElement(qualifiedName)
- Parameters:
qualifiedName – str
This is an overloaded function.
Writes a start element with
qualifiedName
. Subsequent calls towriteAttribute()
will add attributes to this element.See also
- PySide2.QtCore.QXmlStreamWriter.writeTextElement(namespaceUri, name, text)¶
- Parameters:
namespaceUri – str
name – str
text – str
Writes a text element with
name
, 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()
- PySide2.QtCore.QXmlStreamWriter.writeTextElement(qualifiedName, text)
- Parameters:
qualifiedName – str
text – str
This is an overloaded function.
Writes a text element with
qualifiedName
andtext
.This is a convenience function equivalent to:
writeStartElement(qualifiedName) writeCharacters(text) writeEndElement()
© 2022 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 the GNU 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.