CopperSpice API  1.9.1
QXmlFormatter Class Reference

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

Inheritance diagram for QXmlFormatter:
QXmlSerializer QAbstractXmlReceiver

Public Methods

 QXmlFormatter (const QXmlQuery &query, QIODevice *outputDevice)
 
void atomicValue (const QVariant &value) override
 
void attribute (const QXmlName &name, QStringView value) override
 
void characters (QStringView value) override
 
void comment (const QString &value) override
 
void endDocument () override
 
void endElement () override
 
void endOfSequence () override
 
int indentationDepth () const
 
void processingInstruction (const QXmlName &name, const QString &value) override
 
void setIndentationDepth (int depth)
 
void startDocument () override
 
void startElement (const QXmlName &name) override
 
void startOfSequence () override
 
- Public Methods inherited from QXmlSerializer
 QXmlSerializer (const QXmlQuery &query, QIODevice *outputDevice)
 
void atomicValue (const QVariant &value) override
 
void attribute (const QXmlName &name, QStringView value) override
 
void characters (QStringView value) override
 
const QTextCodeccodec () const
 
void comment (const QString &value) override
 
void endDocument () override
 
void endElement () override
 
void endOfSequence () override
 
void namespaceBinding (const QXmlName &nb) override
 
QIODeviceoutputDevice () const
 
void processingInstruction (const QXmlName &name, const QString &value) override
 
void setCodec (const QTextCodec *codec)
 
void startDocument () override
 
void startElement (const QXmlName &name) override
 
void startOfSequence () override
 
- Public Methods inherited from QAbstractXmlReceiver
 QAbstractXmlReceiver ()
 
virtual ~QAbstractXmlReceiver ()
 

Detailed Description

The QXmlFormatter class is an implementation of QXmlSerializer for transforming XQuery output into formatted XML. This class 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 will output the following.

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

In comparison QXmlFormatter outputs the following.

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

If you just want to serialize your XML in a human-readable format use QXmlFormatter The default indentation level is 4 spaces. This value can be set to any indentation value using 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.

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

The indentation style used by QXmlFormatter remains loosely defined. 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);
See also
QXmlSerializer

Constructor & Destructor 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. The given outputDevice is passed directly to QXmlSerializer's constructor.

See also
QXmlSerializer

Method Documentation

void QXmlFormatter::atomicValue ( const QVariant value)
overridevirtual
void QXmlFormatter::attribute ( const QXmlName name,
QStringView  value 
)
overridevirtual
void QXmlFormatter::characters ( QStringView  value)
overridevirtual
void QXmlFormatter::comment ( const QString value)
overridevirtual
void QXmlFormatter::endDocument ( )
overridevirtual
void QXmlFormatter::endElement ( )
overridevirtual
void QXmlFormatter::endOfSequence ( )
overridevirtual
int QXmlFormatter::indentationDepth ( ) const

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

See also
setIndentationDepth()
void QXmlFormatter::processingInstruction ( const QXmlName name,
const QString value 
)
overridevirtual
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()
void QXmlFormatter::startDocument ( )
overridevirtual
void QXmlFormatter::startElement ( const QXmlName name)
overridevirtual
void QXmlFormatter::startOfSequence ( )
overridevirtual