QXmlFormatter Class

The QXmlFormatter class is an implementation of QXmlSerializer for transforming XQuery output into formatted XML. More...

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

Note: All functions in this class are reentrant.

Public Functions

QXmlFormatter(const QXmlQuery &query, QIODevice *outputDevice)
int indentationDepth() const
void setIndentationDepth(int depth)

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 processingInstruction(const QXmlName &name, const QString &value)
virtual void startDocument()
virtual void startElement(const QXmlName &name)
virtual void startOfSequence()

Detailed Description

The QXmlFormatter class is an implementation of QXmlSerializer for transforming XQuery output into formatted XML.

QXmlFormatter is a subclass of QXmlSerializer that formats the XML output to make it easier for humans to read.

QXmlSerializer outputs XML without adding unnecessary whitespace. In particular, it does not add newlines and indentation. To make the XML output easier to read, QXmlFormatter adds newlines and indentation by adding, removing, and modifying sequence nodes that only consist of whitespace. It also modifies whitespace in other places where it is not significant; e.g., between attributes and in the document prologue.

For example, where the base class QXmlSerializer would output this:

<a><b/><c/><p>Some Text</p></a>

QXmlFormatter outputs this:

<a>
   <b/>
   <c/>
   <p>Some Text</p>
</a>

If you just want to serialize your XML in a human-readable format, use QXmlFormatter as it is. The default indentation level is 4 spaces, but you can set your own indentation value setIndentationDepth().

The newlines and indentation added by QXmlFormatter are suitable for common formats, such as XHTML, SVG, or Docbook, where whitespace is not significant. However, if your XML will be used as input where whitespace is significant, then you must write your own subclass of QXmlSerializer or QAbstractXmlReceiver.

Note that using QXmlFormatter instead of QXmlSerializer will increase computational overhead and document storage size due to the insertion of whitespace.

Note also that the indentation style used by QXmlFormatter remains loosely defined and may change in future versions of Qt. If a specific indentation style is required then either use the base class QXmlSerializer directly, or write your own subclass of QXmlSerializer or QAbstractXmlReceiver. Alternatively, you can subclass QXmlFormatter and reimplement the callbacks there.

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

QXmlFormatter formatter(query, myOutputDevice);
formatter.setIndentationDepth(2);
query.evaluateTo(&formatter);

Member Function Documentation

QXmlFormatter::QXmlFormatter(const QXmlQuery &query, QIODevice *outputDevice)

Constructs a formatter that uses the name pool and message handler in query, and writes the result to outputDevice as formatted XML.

outputDevice is passed directly to QXmlSerializer's constructor.

See also QXmlSerializer.

[virtual] void QXmlFormatter::atomicValue(const QVariant &value)

Reimplemented from QAbstractXmlReceiver::atomicValue().

[virtual] void QXmlFormatter::attribute(const QXmlName &name, const QStringRef &value)

Reimplemented from QAbstractXmlReceiver::attribute().

[virtual] void QXmlFormatter::characters(const QStringRef &value)

Reimplemented from QAbstractXmlReceiver::characters().

[virtual] void QXmlFormatter::comment(const QString &value)

Reimplemented from QAbstractXmlReceiver::comment().

[virtual] void QXmlFormatter::endDocument()

Reimplemented from QAbstractXmlReceiver::endDocument().

[virtual] void QXmlFormatter::endElement()

Reimplemented from QAbstractXmlReceiver::endElement().

[virtual] void QXmlFormatter::endOfSequence()

Reimplemented from QAbstractXmlReceiver::endOfSequence().

int QXmlFormatter::indentationDepth() const

Returns the number of spaces QXmlFormatter will output for each indentation level. The default is four.

See also setIndentationDepth().

[virtual] void QXmlFormatter::processingInstruction(const QXmlName &name, const QString &value)

Reimplemented from QAbstractXmlReceiver::processingInstruction().

void QXmlFormatter::setIndentationDepth(int depth)

Sets depth to be the number of spaces QXmlFormatter will output for level of indentation. The default is four.

See also indentationDepth().

[virtual] void QXmlFormatter::startDocument()

Reimplemented from QAbstractXmlReceiver::startDocument().

[virtual] void QXmlFormatter::startElement(const QXmlName &name)

Reimplemented from QAbstractXmlReceiver::startElement().

[virtual] void QXmlFormatter::startOfSequence()

Reimplemented from QAbstractXmlReceiver::startOfSequence().

© 2017 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.