Home · All Classes · Main Classes · Grouped Classes · Modules · Functions

QXmlFormatter Class Reference
[QtXmlPatterns module]

The QXmlFormatter class receives an XQuery sequence and translates it into XML formatted for human reading. More...

 #include <QXmlFormatter>

Inherits QXmlSerializer.

Note: All the functions in this class are reentrant.

This class was introduced in Qt 4.4.

Public Functions

Related Non-Members


Detailed Description

The QXmlFormatter class receives an XQuery sequence and translates it into XML formatted for human reading.

QXmlFormatter is a subclass of QXmlSerializer that formats the XML output for easier human reading. 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 consist of whitespace only. It also modifies whitespace in other places where it is not significant, e.g., between attributes and in the document prolog. 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 format for easy human reading, use QXmlFormatter as is. The default value for indenting each level is 4, but you can set your own indentation value with QXmlFormatter::setIndentationDepth(). The newlines and indentation added by QXmlFormatter are ok for common formats, e.g., XHTML, SVG, or Docbook, where whitespace is not significant. But 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 insertions 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, or you can subclass QXmlFormatter and reimplement the callbacks there.

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

    QXmlFormatterPointer formatter(new QXmlFormatter(query, myOutputDevice));
    formatter->setIndentationDepth(2);
    query.evaluateToReceiver(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.

outputDevice is passed directly to QXmlSerializer's constructor, and the same semantics and expectations applies, as documented in QXmlSerializer's constructor.

int QXmlFormatter::indentationDepth () const

Returns the amount of spaces QXmlFormatter will output for each indentation level.

The default is four.

See also setIndentationDepth().

void QXmlFormatter::setIndentationDepth ( int depth )

Sets the amount of spaces QXmlFormatter writes out for each indentation level to depth.

See also indentationDepth().


Related Non-Members

typedef QXmlFormatterPointer

A typedef for a smart pointer to an instance of QXmlFormatter.


Copyright © 2008 Trolltech Trademarks
Qt 4.4.0-beta1