- Qt 5.5
- Qt XML Patterns
- C++ Classes
- QXmlSerializer
- Qt 5.5.1 Reference Documentation

# QXmlSerializer Class

The QXmlSerializer class is an implementation of QAbstractXmlReceiver for transforming XQuery output into unformatted XML. More...

Header: | #include <QXmlSerializer>
qmake: | QT += xmlpatterns
Since: | Qt 4.4
Inherits: | QAbstractXmlReceiver
Inherited By: | QXmlFormatter

Note: All functions in this class are reentrant.

- List of all members, including inherited members

## Public Functions

| QXmlSerializer(const QXmlQuery & query, QIODevice * outputDevice)
const QTextCodec * | codec() const
QIODevice * | outputDevice() const
void | setCodec(const QTextCodec * outputCodec)

## Reimplemented Public Functions

virtual void | atomicValue(const QVariant & value)
virtual void | attribute(const QXmlName & name, const QStringRef & value)
virtual void | characters(const QStringRef & value)
virtual void | comment(const QString & value)
virtual void | endDocument()
virtual void | endElement()
virtual void | endOfSequence()
virtual void | namespaceBinding(const QXmlName & nb)
virtual void | processingInstruction(const QXmlName & name, const QString & value)
virtual void | startDocument()
virtual void | startElement(const QXmlName & name)
virtual void | startOfSequence()

- 12 public functions inherited from QAbstractXmlReceiver

## Detailed Description

The QXmlSerializer class is an implementation of QAbstractXmlReceiver for transforming XQuery output into unformatted XML.

QXmlSerializer translates an XQuery sequence, usually the output of an QXmlQuery, into XML. Consider the example:

    QXmlQuery query;
    query.setQuery("doc('index.html')/html/body/p[1]");

    QXmlSerializer serializer(query, myOutputDevice);
    query.evaluateTo(&serializer);

First it constructs a query that gets the first paragraph from document `index.html`. Then it constructs an instance of this class with the query and myOutputDevice. Finally, it evaluates the query, producing an ordered sequence of calls to the serializer's callback functions. The sequence of callbacks transforms the query output to XML and writes it to myOutputDevice.

QXmlSerializer will:

- Declare namespaces when needed,
- Use appropriate escaping, when characters can't be represented in the XML,
- Handle line endings appropriately,
- Report errors, when it can't serialize the content, e.g., when asked to serialize an attribute that is a top-level node, or when more than one top-level element is encountered.

If an error occurs during serialization, result is undefined unless the serializer is driven through a call to QXmlQuery::evaluateTo().

If the generated XML should be indented and formatted for reading, use QXmlFormatter.

See also XSLT 2.0 and XQuery 1.0 Serialization and QXmlFormatter.

## Member Function Documentation

### QXmlSerializer::QXmlSerializer(const QXmlQuery & query, QIODevice * outputDevice)

Constructs a serializer that uses the name pool and message handler in query, and writes the output to outputDevice.

outputDevice must be a valid, non-null device that is open in write mode, otherwise behavior is undefined.

outputDevice must not be opened with QIODevice::Text because it will cause the output to be incorrect. This class will ensure line endings are serialized as according with the XML specification. QXmlSerializer does not take ownership of outputDevice.

### `[virtual]` void QXmlSerializer::atomicValue(const QVariant & value)

Reimplemented from QAbstractXmlReceiver::atomicValue().

### `[virtual]` void QXmlSerializer::attribute(const QXmlName & name, const QStringRef & value)

Reimplemented from QAbstractXmlReceiver::attribute().

### `[virtual]` void QXmlSerializer::characters(const QStringRef & value)

Reimplemented from QAbstractXmlReceiver::characters().

### const QTextCodec * QXmlSerializer::codec() const

Returns the codec being used by the serializer for encoding its XML output.

See also setCodec().

### `[virtual]` void QXmlSerializer::comment(const QString & value)

Reimplemented from QAbstractXmlReceiver::comment().

### `[virtual]` void QXmlSerializer::endDocument()

Reimplemented from QAbstractXmlReceiver::endDocument().

### `[virtual]` void QXmlSerializer::endElement()

Reimplemented from QAbstractXmlReceiver::endElement().

### `[virtual]` void QXmlSerializer::endOfSequence()

Reimplemented from QAbstractXmlReceiver::endOfSequence().

### `[virtual]` void QXmlSerializer::namespaceBinding(const QXmlName & nb)

Reimplemented from QAbstractXmlReceiver::namespaceBinding().

### QIODevice * QXmlSerializer::outputDevice() const

Returns a pointer to the output device. There is no corresponding function to set the output device, because the output device must be passed to the constructor. The serializer does not take ownership of its IO device.

### `[virtual]` void QXmlSerializer::processingInstruction(const QXmlName & name, const QString & value)

Reimplemented from QAbstractXmlReceiver::processingInstruction().

### void QXmlSerializer::setCodec(const QTextCodec * outputCodec)

Sets the codec the serializer will use for encoding its XML output. The output codec is set to outputCodec. By default, the output codec is set to the one for `UTF-8`. The serializer does not take ownership of the codec.

See also codec().

### `[virtual]` void QXmlSerializer::startDocument()

Reimplemented from QAbstractXmlReceiver::startDocument().

### `[virtual]` void QXmlSerializer::startElement(const QXmlName & name)

Reimplemented from QAbstractXmlReceiver::startElement().

### `[virtual]` void QXmlSerializer::startOfSequence()

Reimplemented from QAbstractXmlReceiver::startOfSequence().