class QXmlStreamWriter

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

Synopsis

Methods

Note

This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE

Detailed Description

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

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:

stream = QXmlStreamWriter(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() .

QXmlStreamWriter always encodes XML in UTF-8.

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 .

__init__()

Constructs a stream writer.

See also

setDevice()

__init__(array)
Parameters:

arrayQByteArray

Constructs a stream writer that writes into array. This is the same as creating an xml writer that operates on a QBuffer device which in turn operates on array.

__init__(device)
Parameters:

deviceQIODevice

Constructs a stream writer that writes into device;

autoFormatting()
Return type:

bool

Returns true if auto formatting is enabled, otherwise false.

autoFormattingIndent()
Return type:

int

device()
Return type:

QIODevice

Returns the current device associated with the QXmlStreamWriter , or None if no device has been assigned.

See also

setDevice()

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.

setAutoFormatting(arg__1)
Parameters:

arg__1 – bool

Enables auto formatting if enable is true, otherwise disables it.

The default value is false.

See also

autoFormatting()

setAutoFormattingIndent(spacesOrTabs)
Parameters:

spacesOrTabs – int

setDevice(device)
Parameters:

deviceQIODevice

Sets the current device to device. If you want the stream to write into a QByteArray , you can create a QBuffer device.

See also

device()

writeAttribute(attribute)
Parameters:

attributeQXmlStreamAttribute

This is an overloaded function.

Writes the attribute.

This function can only be called after writeStartElement() before any content is written, or after writeEmptyElement() .

writeAttribute(qualifiedName, value)
Parameters:
  • qualifiedName – str

  • value – str

This is an overloaded function.

Writes an attribute with qualifiedName and value.

This function can only be called after writeStartElement() before any content is written, or after writeEmptyElement() .

Note

In Qt versions prior to 6.5, this function took QString , not QAnyStringView .

writeAttribute(namespaceUri, name, value)
Parameters:
  • namespaceUri – str

  • name – str

  • value – str

Writes an attribute with name and value, prefixed for the specified namespaceUri. 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 after writeEmptyElement() .

Note

In Qt versions prior to 6.5, this function took QString , not QAnyStringView .

writeAttributes(attributes)
Parameters:

attributesQXmlStreamAttributes

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 after writeEmptyElement() .

writeCDATA(text)
Parameters:

text – str

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 writeCharacters() automatically escapes all non-content characters.

Note

In Qt versions prior to 6.5, this function took QString , not QAnyStringView .

writeCharacters(text)
Parameters:

text – str

Writes text. The characters “<”, “&”, and “”” are escaped as entity references “&lt;”, “&amp;, and “&quot;”. To avoid the forbidden sequence “]]>”, “>” is also escaped as “&gt;”.

Note

In Qt versions prior to 6.5, this function took QString , not QAnyStringView .

writeComment(text)
Parameters:

text – str

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.

Note

In Qt versions prior to 6.5, this function took QString , not QAnyStringView .

writeCurrentToken(reader)
Parameters:

readerQXmlStreamReader

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

tokenType()

writeDTD(dtd)
Parameters:

dtd – str

Writes a DTD section. The dtd represents the entire doctypedecl production from the XML 1.0 specification.

Note

In Qt versions prior to 6.5, this function took QString , not QAnyStringView .

writeDefaultNamespace(namespaceUri)
Parameters:

namespaceUri – str

Writes a default namespace declaration for namespaceUri.

If writeStartElement() or writeEmptyElement() 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.

Note

In Qt versions prior to 6.5, this function took QString , not QAnyStringView .

writeEmptyElement(qualifiedName)
Parameters:

qualifiedName – str

This is an overloaded function.

Writes an empty element with qualified name qualifiedName. Subsequent calls to writeAttribute() will add attributes to this element.

Note

In Qt versions prior to 6.5, this function took QString , not QAnyStringView .

writeEmptyElement(namespaceUri, name)
Parameters:
  • namespaceUri – str

  • name – str

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

In Qt versions prior to 6.5, this function took QString , not QAnyStringView .

See also

writeNamespace()

writeEndDocument()

Closes all remaining open start elements and writes a newline.

writeEndElement()

Closes the previous start element.

writeEntityReference(name)
Parameters:

name – str

Writes the entity reference name to the stream, as “&``name``;”.

Note

In Qt versions prior to 6.5, this function took QString , not QAnyStringView .

writeNamespace(namespaceUri[, prefix={}])
Parameters:
  • namespaceUri – str

  • prefix – str

Writes a namespace declaration for namespaceUri with prefix. If prefix is empty, QXmlStreamWriter assigns a unique prefix consisting of the letter ‘n’ followed by a number.

If writeStartElement() or writeEmptyElement() 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.

Note

In Qt versions prior to 6.5, this function took QString , not QAnyStringView .

writeProcessingInstruction(target[, data={}])
Parameters:
  • target – str

  • data – str

Writes an XML processing instruction with target and data, where data must not contain the sequence “?>”.

Note

In Qt versions prior to 6.5, this function took QString , not QAnyStringView .

writeStartDocument()

This is an overloaded function.

Writes a document start with XML version number “1.0”.

writeStartDocument(version)
Parameters:

version – str

Writes a document start with the XML version number version.

Note

In Qt versions prior to 6.5, this function took QString , not QAnyStringView .

writeStartDocument(version, standalone)
Parameters:
  • version – str

  • standalone – bool

Writes a document start with the XML version number version and a standalone attribute standalone.

Note

In Qt versions prior to 6.5, this function took QString , not QAnyStringView .

writeStartElement(qualifiedName)
Parameters:

qualifiedName – str

This is an overloaded function.

Writes a start element with qualifiedName. Subsequent calls to writeAttribute() will add attributes to this element.

Note

In Qt versions prior to 6.5, this function took QString , not QAnyStringView .

writeStartElement(namespaceUri, name)
Parameters:
  • namespaceUri – str

  • name – str

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.

Note

In Qt versions prior to 6.5, this function took QString , not QAnyStringView .

writeTextElement(qualifiedName, text)
Parameters:
  • qualifiedName – str

  • text – str

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

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()

Note

In Qt versions prior to 6.5, this function took QString , not QAnyStringView .

writeTextElement(namespaceUri, name, text)
Parameters:
  • namespaceUri – str

  • name – str

  • text – str

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

Writes a text element with name, prefixed for the specified namespaceUri, and text. 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()

Note

In Qt versions prior to 6.5, this function took QString , not QAnyStringView .