PySide.QtXml.QDomDocument class represents an XML document.
PySide.QtXml.QDomDocument class represents the entire XML document. Conceptually, it is the root of the document tree, and provides the primary access to the document's data.
由于元素、文本节点、注释、处理指令、等不能存在于文档上下文之外,因此文档类还包含创建这些对象所需的工厂函数。创建的节点对象拥有 PySide.QtXml.QDomNode.ownerDocument() function which associates them with the document within whose context they were created. The DOM classes that will be used most often are PySide.QtXml.QDomNode , PySide.QtXml.QDomDocument , PySide.QtXml.QDomElement and PySide.QtXml.QDomText .
经剖析的 XML 通过对象树在内部表示,可以使用各种 QDom 类访问它们。所有 QDom 类只 reference objects in the internal tree. The internal objects in the DOM tree will get deleted once the last QDom object referencing them and the PySide.QtXml.QDomDocument itself are deleted.
元素、文本节点、等的创建是使用此类中提供的各种工厂函数完成的。使用 QDom 类默认构造函数只会导致无法操纵或插入文档的空对象。
PySide.QtXml.QDomDocument class has several functions for creating document data, for example, PySide.QtXml.QDomDocument.createElement() , PySide.QtXml.QDomDocument.createTextNode() , PySide.QtXml.QDomDocument.createComment() , PySide.QtXml.QDomDocument.createCDATASection() , PySide.QtXml.QDomDocument.createProcessingInstruction() , PySide.QtXml.QDomDocument.createAttribute() and PySide.QtXml.QDomDocument.createEntityReference() . Some of these functions have versions that support namespaces, i.e. PySide.QtXml.QDomDocument.createElementNS() and PySide.QtXml.QDomDocument.createAttributeNS() 。 PySide.QtXml.QDomDocument.createDocumentFragment() function is used to hold parts of the document; this is useful for manipulating for complex documents.
整个文档内容的设置采用 PySide.QtXml.QDomDocument.setContent() . This function parses the string it is passed as an XML document and creates the DOM tree that represents the document. The root element is available using PySide.QtXml.QDomDocument.documentElement() . The textual representation of the document can be obtained using PySide.QtXml.QDomDocument.toString() .
注意
The DOM tree might end up reserving a lot of memory if the XML document is big. For big XML documents, the PySide.QtCore.QXmlStreamReader 或 PySide.QtXmlPatterns.QXmlQuery classes might be better solutions.
它是可能的,将来自另一文档的节点插入文档使用 PySide.QtXml.QDomDocument.importNode() .
可以获取拥有特定标签的所有元素的列表使用 PySide.QtXml.QDomDocument.elementsByTagName() or with PySide.QtXml.QDomDocument.elementsByTagNameNS() .
QDom 类的使用通常如下所示:
doc = QDomDocument("mydocument")
file = QFile("mydocument.xml")
if not file.open(QIODevice::ReadOnly):
return
if not doc.setContent(&file):
file.close()
return
file.close()
# print out the element names of all elements that are direct children
# of the outermost element.
docElem = doc.documentElement()
n = docElem.firstChild()
while not n.isNull():
e = n.toElement() # try to convert the node to an element.
if not e.isNull():
print e.tagName() # the node really is an element.
n = n.nextSibling()
}
# Here we append a new element to the end of the document
elem = doc.createElement("img")
elem.setAttribute("src", "myimage.png")
docElem.appendChild(elem)
一旦 doc and elem 超出作用域,表示 XML 文档的整个内部树就会被删除。
要使用 DOM 创建文档,所用代码像这样:
doc = QDomDocument("MyML")
root = doc.createElement("MyML")
doc.appendChild(root)
tag = doc.createElement("Greeting")
root.appendChild(tag)
t = doc.createTextNode("Hello World")
tag.appendChild(t)
xml = doc.toString()
For further information about the Document Object Model see the Document Object Model (DOM) Level 1 and Level 2 Core Specifications.
另请参阅
DOM 书签范例 简单 DOM 模型范例
| 参数: |
|
|---|
构造空文档。
构造副本为 x .
拷贝数据是共享的 (浅拷贝):修改一个节点也将改变另一节点。若想要制作深度副本,使用 PySide.QtXml.QDomNode.cloneNode() .
创建文档采用文档类型 doctype .
创建文档并将文档类型名称设为 name .
| 参数: | name – unicode |
|---|---|
| 返回类型: | PySide.QtXml.QDomAttr |
创建新属性称为 name 可以插入元素,如:使用 QDomElement.setAttributeNode() .
若 name 不是有效 XML 名称,此函数的行为被支配由 QDomImplementation.InvalidDataPolicy .
| 参数: |
|
|---|---|
| 返回类型: |
创建具有名称空间支持,可以插入元素的新属性。属性名称为 qName 和名称空间 URI 为 nsURI 。此特征还设置 QDomNode.prefix() and QDomNode.localName() to appropriate values (depending on qName ).
若 qName 不是有效 XML 名称,此函数的行为被支配由 QDomImplementation.InvalidDataPolicy .
| 参数: | data – unicode |
|---|---|
| 返回类型: | PySide.QtXml.QDomCDATASection |
创建新 CDATA 区间为字符串 value 可以插入文档,如:使用 QDomNode.appendChild() .
若 value 包含无法存储在 CDATA 区间的字符,此函数的行为被支配由 QDomImplementation.InvalidDataPolicy .
| 参数: | data – unicode |
|---|---|
| 返回类型: | PySide.QtXml.QDomComment |
创建的新注释为字符串 value 可以插入文档,如:使用 QDomNode.appendChild() .
若 value 包含无法存储于 XML 注释中的字符,此函数的行为被支配由 QDomImplementation.InvalidDataPolicy .
| 返回类型: | PySide.QtXml.QDomDocumentFragment |
|---|
创建新的文档片段,可以用于保持文档的一部分,如:当对文档树做复杂操纵时。
| 参数: | tagName – unicode |
|---|---|
| 返回类型: | PySide.QtXml.QDomElement |
创建的新元素称为 tagName 可以插入 DOM 树,如:使用 QDomNode.appendChild() .
若 tagName 不是有效 XML 名称,此函数的行为被支配由 QDomImplementation.InvalidDataPolicy .
| 参数: |
|
|---|---|
| 返回类型: |
创建具有名称空间支持的新元素,可以插入 DOM 树。元素的名称为 qName 和名称空间 URI 为 nsURI 。此特征还设置 QDomNode.prefix() and QDomNode.localName() to appropriate values (depending on qName ).
若 qName 为空字符串,返回 null 元素不管无效数据策略是否有设置。
| 参数: | name – unicode |
|---|---|
| 返回类型: | PySide.QtXml.QDomEntityReference |
创建新实体引用称为 name 可以插入文档,如:使用 QDomNode.appendChild() .
若 name 不是有效 XML 名称,此函数的行为被支配由 QDomImplementation.InvalidDataPolicy .
| 参数: |
|
|---|---|
| 返回类型: |
创建可以插入文档的新处理指令,如:使用 QDomNode.appendChild() . This function sets the target for the processing instruction to target 和数据到 data .
若 target 不是有效 XML 名称,或数据若包含不可以出现在处理指令中的字符,此函数的行为被支配由 QDomImplementation.InvalidDataPolicy .
| 参数: | data – unicode |
|---|---|
| 返回类型: | PySide.QtXml.QDomText |
创建文本节点为字符串 value 可以插入文档树,如:使用 QDomNode.appendChild() .
若 value 包含不可以作为 XML 文档字符数据存储的字符 (即使按字符引用形式),此函数的行为被支配由 QDomImplementation.InvalidDataPolicy .
| 返回类型: | PySide.QtXml.QDomDocumentType |
|---|
返回此文档的文档类型。
| 返回类型: | PySide.QtXml.QDomElement |
|---|
返回文档的根元素。
| 参数: | elementId – unicode |
|---|---|
| 返回类型: | PySide.QtXml.QDomElement |
返回的元素 ID 等于 elementId 。若未找到具有 ID 的元素,此函数返回 null element .
由于 QDomClasses 不知道哪些属性是元素 ID,所以此函数始终返回 null element 。这在未来版本中可能改变。
| 参数: | tagname – unicode |
|---|---|
| 返回类型: | PySide.QtXml.QDomNodeList |
返回 PySide.QtXml.QDomNodeList ,包含文档中的所有元素具有名称 tagname 。节点列表次序是以预顺序遍历元素树时遇到它们的次序。
| 参数: |
|
|---|---|
| 返回类型: |
返回 PySide.QtXml.QDomNodeList ,包含文档中的所有元素具有本地名称 localName 和名称空间 URI 为 nsURI 。节点列表次序是以预顺序遍历元素树时遇到它们的次序。
| 返回类型: | PySide.QtXml.QDomImplementation |
|---|
| 参数: |
|
|---|---|
| 返回类型: |
导入节点 importedNode 从另一文档到此文档。 importedNode 仍然在原文档中;此函数创建可在此文档中使用的副本。
此函数返回属于此文档的导入节点。返回节点没有父级。它是不可能的去导入 PySide.QtXml.QDomDocument and PySide.QtXml.QDomDocumentType 节点。在那些情况下,此函数返回 null node .
若 deep 为 True,此函数不仅导入节点 importedNode 及其整个子树;若它为 false,仅 importedNode 被导入。自变量 deep 不影响 PySide.QtXml.QDomAttr and PySide.QtXml.QDomEntityReference 节点,因为后代的 PySide.QtXml.QDomAttr 节点始终被导入而那些 PySide.QtXml.QDomEntityReference 节点从不被导入。
此函数行为稍有不同,根据节点类型:
| 节点类型 | 行为 |
| PySide.QtXml.QDomAttr | owner 元素被设为 0 并将生成属性中的指定标志设为 true。整个子树的 importedNode 始终为属性节点导入: deep 不起作用。 |
| PySide.QtXml.QDomDocument | 无法导入文档节点。 |
| PySide.QtXml.QDomDocumentFragment | 若 deep 为 True,此函数导入整个文档片段;否则它仅生成空文档片段。 |
| PySide.QtXml.QDomDocumentType | 无法导入文档类型节点。 |
| PySide.QtXml.QDomElement | 属性为 QDomAttr.specified() is true are also imported, other attributes are not imported. If deep 为 True,此函数还导入子树的 importedNode ;否则它只导入元素节点 (和一些属性,见上文)。 |
| PySide.QtXml.QDomEntity | 实体节点可以导入,但目前没有办法使用它们,因为文档类型在 DOM 级别 2 中是只读的。 |
| PySide.QtXml.QDomEntityReference | 实体引用节点的后代从不被导入: deep 不起作用。 |
| PySide.QtXml.QDomNotation | 表示法节点可以导入,但目前没有办法使用它们,因为文档类型在 DOM 级别 2 中是只读的。 |
| PySide.QtXml.QDomProcessingInstruction | 处理指令的目标和值被拷贝到新节点。 |
| PySide.QtXml.QDomText | 文本被拷贝到新节点。 |
| PySide.QtXml.QDomCDATASection | 文本被拷贝到新节点。 |
| PySide.QtXml.QDomComment | 文本被拷贝到新节点。 |
QDomNode.insertAfter() QDomNode.replaceChild() QDomNode.removeChild() QDomNode.appendChild()
| 参数: |
|
|---|---|
| 返回类型: |
(retval, errorMsg, errorLine, errorColumn) |
此函数剖析 XML 文档从字节数组 data 并将它设为文档内容。它试着按 XML 规范要求检测文档编码。
若 namespaceProcessing 为 true,剖析器识别 XML 文件中的名称空间并将前缀名称、本地名称和名称空间 URI 设为适当值。若 namespaceProcessing 为 false,剖析器不处理名称空间当它读取 XML 文件时。
If a parse error occurs, this function returns false and the error message is placed in * errorMsg ,行号在 * errorLine 和列号在 * errorColumn (unless the associated pointer is set to 0); otherwise this function returns true. The various error messages are described in the PySide.QtXml.QXmlParseException 类文档编制。注意:若想要向应用程序用户显示这些错误消息,将以英文显示它们除非它们被明确翻译。
若 namespaceProcessing 为 true,函数 QDomNode.prefix() returns a string for all elements and attributes. It returns an empty string if the element or attribute has no prefix.
仅由空格组成的文本节点被剥离,且不会出现在 PySide.QtXml.QDomDocument . If this behavior is not desired, one can use the PySide.QtXml.QDomDocument.setContent() overload that allows a PySide.QtXml.QXmlReader 提供。
若 namespaceProcessing 为 false,函数 QDomNode.prefix() , QDomNode.localName() and QDomNode.namespaceURI() return an empty string.
实体引用处理如下:
QDomNode.prefix() QString.isNull() QString.isEmpty()
| 参数: | text – unicode |
|---|---|
| 返回类型: | (retval, errorMsg, errorLine, errorColumn) |
这是重载函数。
此函数读取 XML 文档从字符串 text , returning true if the content was successfully parsed; otherwise returns false. Since text 已经是 Unicode 字符串,不用履行编码检测。
也不履行名称空间处理。
| 参数: |
|
|---|---|
| 返回类型: |
(retval, errorMsg, errorLine, errorColumn) |
这是重载函数。
此函数读取 XML 文档从字符串 text , returning true if the content was successfully parsed; otherwise returns false. Since text 已经是 Unicode 字符串,不用做编码检测。
| 参数: | text – PySide.QtCore.QByteArray |
|---|---|
| 返回类型: | (retval, errorMsg, errorLine, errorColumn) |
这是重载函数。
此函数读取 XML 文档从字节数组 buffer ,返回 true 若内容被成功剖析;否则返回 false。
不履行名称空间处理。
| 参数: |
|
|---|---|
| 返回类型: |
(retval, errorMsg, errorLine, errorColumn) |
这是重载函数。
此函数读取 XML 文档从 IO 设备 dev ,返回 true 若内容被成功剖析;否则返回 false。
| 参数: | dev – PySide.QtCore.QIODevice |
|---|---|
| 返回类型: | (retval, errorMsg, errorLine, errorColumn) |
这是重载函数。
此函数读取 XML 文档从 IO 设备 dev ,返回 true 若内容被成功剖析;否则返回 false。
不履行名称空间处理。
| 参数: |
|
|---|---|
| 返回类型: |
(retval, errorMsg, errorLine, errorColumn) |
这是重载函数。
此函数读取 XML 文档从 PySide.QtXml.QXmlInputSource source ,返回 true 若内容被成功剖析;否则返回 false。
| 参数: |
|
|---|---|
| 返回类型: |
(retval, errorMsg, errorLine, errorColumn) |
这是重载函数。
此函数读取 XML 文档从 PySide.QtXml.QXmlInputSource source and parses it with the PySide.QtXml.QXmlReader reader ,返回 true 若内容被成功剖析;否则返回 false。
This function doesn't change the features of the reader . If you want to use certain features for parsing you can use this function to set up the reader appropriately.
| 参数: | arg__1 – PySide.QtCore.int |
|---|---|
| 返回类型: | PySide.QtCore.QByteArray |
将经剖析文档转换回其正文表示并返回 PySide.QtCore.QByteArray 包含编码为 UTF-8 的数据。
此函数使用 indent 作为缩进子元素的空格数量。
| 参数: | arg__1 – PySide.QtCore.int |
|---|---|
| 返回类型: | unicode |
将经剖析文档转换回其正文表示。
此函数使用 indent 作为缩进子元素的空格数量。
若 indent 为 -1,根本不添加空格。