内容表

QXmlFormatter

详细描述

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

PySide.QtXmlPatterns.QXmlFormatter 是子类化的 PySide.QtXmlPatterns.QXmlSerializer that formats the XML output to make it easier for humans to read.

PySide.QtXmlPatterns.QXmlSerializer outputs XML without adding unnecessary whitespace. In particular, it does not add newlines and indentation. To make the XML output easier to read, PySide.QtXmlPatterns.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 PySide.QtXmlPatterns.QXmlSerializer would output this:

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

PySide.QtXmlPatterns.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 PySide.QtXmlPatterns.QXmlFormatter as it is. The default indentation level is 4 spaces, but you can set your own indentation value PySide.QtXmlPatterns.QXmlFormatter.setIndentationDepth() .

newlines and indentation added by PySide.QtXmlPatterns.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 PySide.QtXmlPatterns.QXmlSerializer or PySide.QtXmlPatterns.QAbstractXmlReceiver .

Note that using PySide.QtXmlPatterns.QXmlFormatter 而不是 PySide.QtXmlPatterns.QXmlSerializer will increase computational overhead and document storage size due to the insertion of whitespace.

Note also that the indentation style used by PySide.QtXmlPatterns.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 PySide.QtXmlPatterns.QXmlSerializer directly, or write your own subclass of PySide.QtXmlPatterns.QXmlSerializer or PySide.QtXmlPatterns.QAbstractXmlReceiver . Alternatively, you can subclass PySide.QtXmlPatterns.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);
									
class PySide.QtXmlPatterns. QXmlFormatter ( query , 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 PySide.QtXmlPatterns.QXmlSerializer ‘s constructor.

PySide.QtXmlPatterns.QXmlFormatter. indentationDepth ( )
返回类型: PySide.QtCore.int

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

PySide.QtXmlPatterns.QXmlFormatter. setIndentationDepth ( depth )
参数: depth PySide.QtCore.int

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

PySide.QtXmlPatterns.QXmlFormatter. startFormattingContent ( )