QXmlFormatter Class

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

头: #include <QXmlFormatter>
qmake: QT += xmlpatterns
Since: Qt 4.4
继承: QXmlSerializer

注意: 此类的所有函数 可重入 .

公共函数

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

重实现公共函数

virtual void atomicValue (const QVariant & value )
virtual void 属性 (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 ()

详细描述

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

QXmlFormatter 是子类化的 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 而不是 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);
					

成员函数文档编制

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 的构造函数。

另请参阅 QXmlSerializer .

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

重实现自 QAbstractXmlReceiver::atomicValue ().

[虚拟] void QXmlFormatter:: 属性 (const QXmlName & name , const QStringRef & value )

重实现自 QAbstractXmlReceiver::attribute ().

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

重实现自 QAbstractXmlReceiver::characters ().

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

重实现自 QAbstractXmlReceiver::comment ().

[虚拟] void QXmlFormatter:: endDocument ()

重实现自 QAbstractXmlReceiver::endDocument ().

[虚拟] void QXmlFormatter:: endElement ()

重实现自 QAbstractXmlReceiver::endElement ().

[虚拟] void QXmlFormatter:: endOfSequence ()

重实现自 QAbstractXmlReceiver::endOfSequence ().

int QXmlFormatter:: indentationDepth () const

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

另请参阅 setIndentationDepth ().

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

重实现自 QAbstractXmlReceiver::processingInstruction ().

void QXmlFormatter:: setIndentationDepth ( int depth )

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

另请参阅 indentationDepth ().

[虚拟] void QXmlFormatter:: startDocument ()

重实现自 QAbstractXmlReceiver::startDocument ().

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

重实现自 QAbstractXmlReceiver::startElement ().

[虚拟] void QXmlFormatter:: startOfSequence ()

重实现自 QAbstractXmlReceiver::startOfSequence ().