IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

QXmlStreamWriter Class

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

All functions in this class are reentrant.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

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, qtserialization

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.

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:

 
Sélectionnez
    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 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.

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

By default, auto-formatting is disabled.

Access functions:

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 autoFormatting

Member Function Documentation

 

QXmlStreamWriter::QXmlStreamWriter()

Constructs a stream writer.

See Also

See also setDevice()

[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 QBuffer 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 autoFormatting.

See Also

See also setAutoFormatting()

QIODevice *QXmlStreamWriter::device() const

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

See Also

See also setDevice()

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

See Also

See also autoFormatting()

void QXmlStreamWriter::setDevice(QIODevice *device)

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

See Also

See also device()

void QXmlStreamWriter::writeAttribute(const QXmlStreamAttribute &attribute)

This is an overloaded function.

Writes the attribute.

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

void QXmlStreamWriter::writeAttributes(const QXmlStreamAttributes &attributes)

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

See Also

See also writeAttribute(), writeNamespace()

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

void QXmlStreamWriter::writeEndDocument()

Closes all remaining open start elements and writes a newline.

See Also

See also writeStartDocument()

void QXmlStreamWriter::writeEndElement()

Closes the previous start element.

See Also

See also writeStartElement()

void QXmlStreamWriter::writeStartDocument()

This is an overloaded function.

Writes a document start with XML version number "1.0".

See Also

See also writeEndDocument()

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+