内容表

上一话题

QDomComment

下一话题

QDomDocumentFragment

QDomDocument

QDomDocument class represents an XML document. 更多

Inheritance diagram of PySide2.QtXml.QDomDocument

概要

函数

详细描述

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.

由于元素、文本节点、注释、处理指令、等不能存在于文档上下文之外,因此文档类还包含创建这些对象所需的工厂函数。创建的节点对象拥有 ownerDocument() function which associates them with the document within whose context they were created. The DOM classes that will be used most often are QDomNode , QDomDocument , QDomElement and 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 or the QDomDocument itself is deleted.

元素、文本节点、等的创建是使用此类中提供的各种工厂函数完成的。使用 QDom 类默认构造函数只会导致无法操纵或插入文档的空对象。

QDomDocument class has several functions for creating document data, for example, createElement() , createTextNode() , createComment() , createCDATASection() , createProcessingInstruction() , createAttribute() and createEntityReference() . Some of these functions have versions that support namespaces, i.e. createElementNS() and createAttributeNS() createDocumentFragment() function is used to hold parts of the document; this is useful for manipulating for complex documents.

整个文档内容的设置采用 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 documentElement() . The textual representation of the document can be obtained using toString() .

注意

DOM 树可能最终预留大量内存,若 XML 文档很大。对于此类文档, QXmlStreamReader QXmlQuery classes might be better solutions.

它是可能的,将来自另一文档的节点插入文档使用 importNode() .

可以获取拥有特定标签的所有元素的列表使用 elementsByTagName() or with 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()
											

有关文档对象模型的进一步信息,见 DOM (文档对象模型) 级别 1 and 级别 2 核心 规范。

class QDomDocument

QDomDocument(x)

QDomDocument(doctype)

QDomDocument(name)

param x

QDomDocument

param doctype

QDomDocumentType

param name

unicode

构造空文档。

PySide2.QtXml.QDomDocument. createAttribute ( name )
参数

name – unicode

返回类型

QDomAttr

创建新属性称为 name 可以插入元素,如:使用 setAttributeNode() .

name 不是有效 XML 名称,此函数的行为被支配由 InvalidDataPolicy .

PySide2.QtXml.QDomDocument. createAttributeNS ( nsURI , qName )
参数
  • nsURI – unicode

  • qName – unicode

返回类型

QDomAttr

创建具有名称空间支持,可以插入元素的新属性。属性名称为 qName 和名称空间 URI 为 nsURI 。此特征还设置 prefix() and localName() to appropriate values (depending on qName ).

qName 不是有效 XML 名称,此函数的行为被支配由 InvalidDataPolicy .

另请参阅

createAttribute()

PySide2.QtXml.QDomDocument. createCDATASection ( data )
参数

data – unicode

返回类型

QDomCDATASection

创建新 CDATA 区间为字符串 value 可以插入文档,如:使用 appendChild() .

value 包含无法存储在 CDATA 区间的字符,此函数的行为被支配由 InvalidDataPolicy .

PySide2.QtXml.QDomDocument. createComment ( data )
参数

data – unicode

返回类型

QDomComment

创建的新注释为字符串 value 可以插入文档,如:使用 appendChild() .

value 包含无法存储于 XML 注释中的字符,此函数的行为被支配由 InvalidDataPolicy .

PySide2.QtXml.QDomDocument. createDocumentFragment ( )
返回类型

QDomDocumentFragment

创建新的文档片段,可以用于保持文档的一部分,如:当对文档树做复杂操纵时。

PySide2.QtXml.QDomDocument. createElement ( tagName )
参数

tagName – unicode

返回类型

QDomElement

创建的新元素称为 tagName 可以插入 DOM 树,如:使用 appendChild() .

tagName 不是有效 XML 名称,此函数的行为被支配由 InvalidDataPolicy .

PySide2.QtXml.QDomDocument. createElementNS ( nsURI , qName )
参数
  • nsURI – unicode

  • qName – unicode

返回类型

QDomElement

创建具有名称空间支持的新元素,可以插入 DOM 树。元素的名称为 qName 和名称空间 URI 为 nsURI 。此特征还设置 prefix() and localName() to appropriate values (depending on qName ).

qName 为空字符串,返回 null 元素不管无效数据策略是否有设置。

另请参阅

createElement()

PySide2.QtXml.QDomDocument. createEntityReference ( name )
参数

name – unicode

返回类型

QDomEntityReference

创建新实体引用称为 name 可以插入文档,如:使用 appendChild() .

name 不是有效 XML 名称,此函数的行为被支配由 InvalidDataPolicy .

PySide2.QtXml.QDomDocument. createProcessingInstruction ( target , data )
参数
  • target – unicode

  • data – unicode

返回类型

QDomProcessingInstruction

创建可以插入文档的新处理指令,如:使用 appendChild() . This function sets the target for the processing instruction to target 和数据到 data .

target 不是有效 XML 名称,或数据若包含不可以出现在处理指令中的字符,此函数的行为被支配由 InvalidDataPolicy .

PySide2.QtXml.QDomDocument. createTextNode ( data )
参数

data – unicode

返回类型

QDomText

创建文本节点为字符串 value 可以插入文档树,如:使用 appendChild() .

value 包含不可以作为 XML 文档字符数据存储的字符 (即使按字符引用形式),此函数的行为被支配由 InvalidDataPolicy .

PySide2.QtXml.QDomDocument. doctype ( )
返回类型

QDomDocumentType

返回此文档的文档类型。

PySide2.QtXml.QDomDocument. documentElement ( )
返回类型

QDomElement

返回文档的根元素。

PySide2.QtXml.QDomDocument. elementById ( elementId )
参数

elementId – unicode

返回类型

QDomElement

返回的元素 ID 等于 elementId 。若未找到具有 ID 的元素,此函数返回 null element .

由于 QDomClasses 不知道哪些属性是元素 ID,所以此函数始终返回 null element 。这在未来版本中可能改变。

PySide2.QtXml.QDomDocument. elementsByTagName ( tagname )
参数

tagname – unicode

返回类型

QDomNodeList

返回 QDomNodeList ,包含文档中的所有元素具有名称 tagname 。节点列表次序是以预顺序遍历元素树时遇到它们的次序。

PySide2.QtXml.QDomDocument. elementsByTagNameNS ( nsURI , localName )
参数
  • nsURI – unicode

  • localName – unicode

返回类型

QDomNodeList

返回 QDomNodeList ,包含文档中的所有元素具有本地名称 localName 和名称空间 URI 为 nsURI 。节点列表次序是以预顺序遍历元素树时遇到它们的次序。

PySide2.QtXml.QDomDocument. 实现 ( )
返回类型

QDomImplementation

返回 QDomImplementation 对象。

PySide2.QtXml.QDomDocument. importNode ( importedNode , deep )
参数
返回类型

QDomNode

导入节点 importedNode 从另一文档到此文档。 importedNode 仍然在原文档中;此函数创建可在此文档中使用的副本。

此函数返回属于此文档的导入节点。返回节点没有父级。它是不可能的去导入 QDomDocument and QDomDocumentType 节点。在那些情况下,此函数返回 null node .

importedNode null node ,返回空节点。

deep 为 True,此函数不仅导入节点 importedNode 及其整个子树;若它为 false,仅 importedNode 被导入。自变量 deep 不影响 QDomAttr and QDomEntityReference 节点,因为后代的 QDomAttr 节点始终被导入而那些 QDomEntityReference 节点从不被导入。

此函数行为稍有不同,根据节点类型:

节点类型

行为

QDomAttr

owner 元素被设为 0 并将生成属性中的指定标志设为 true。整个子树的 importedNode 始终为属性节点导入: deep 不起作用。

QDomDocument

无法导入文档节点。

QDomDocumentFragment

deep 为 True,此函数导入整个文档片段;否则它仅生成空文档片段。

QDomDocumentType

无法导入文档类型节点。

QDomElement

属性为 specified() is true are also imported, other attributes are not imported. If deep 为 True,此函数还导入子树的 importedNode ;否则它只导入元素节点 (和一些属性,见上文)。

QDomEntity

实体节点可以导入,但目前没有办法使用它们,因为文档类型在 DOM 级别 2 中是只读的。

QDomEntityReference

实体引用节点的后代从不被导入: deep 不起作用。

QDomNotation

表示法节点可以导入,但目前没有办法使用它们,因为文档类型在 DOM 级别 2 中是只读的。

QDomProcessingInstruction

处理指令的目标和值被拷贝到新节点。

QDomText

文本被拷贝到新节点。

QDomCDATASection

文本被拷贝到新节点。

QDomComment

文本被拷贝到新节点。

PySide2.QtXml.QDomDocument. setContent ( text , namespaceProcessing )
参数
  • text – unicode

  • namespaceProcessing bool

返回类型

(retval, errorMsg, errorLine, errorColumn)

PySide2.QtXml.QDomDocument. setContent ( text )
参数

text – unicode

返回类型

(retval, errorMsg, errorLine, errorColumn)

PySide2.QtXml.QDomDocument. setContent ( source , reader )
参数
返回类型

(retval, errorMsg, errorLine, errorColumn)

注意

此函数被弃用。

这是重载函数。

此函数读取 XML 文档从 QXmlInputSource source and parses it with the 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.

另请参阅

QXmlSimpleReader

PySide2.QtXml.QDomDocument. setContent ( text , namespaceProcessing )
参数
  • text QByteArray

  • namespaceProcessing bool

返回类型

(retval, errorMsg, errorLine, errorColumn)

PySide2.QtXml.QDomDocument. setContent ( text )
参数

text QByteArray

返回类型

(retval, errorMsg, errorLine, errorColumn)

PySide2.QtXml.QDomDocument. setContent ( source , namespaceProcessing )
参数
返回类型

(retval, errorMsg, errorLine, errorColumn)

注意

此函数被弃用。

这是重载函数。

此函数读取 XML 文档从 QXmlInputSource source ,返回 true 若内容被成功剖析;否则返回 false .

PySide2.QtXml.QDomDocument. setContent ( dev , namespaceProcessing )
参数
  • dev QIODevice

  • namespaceProcessing bool

返回类型

(retval, errorMsg, errorLine, errorColumn)

这是重载函数。

此函数读取 XML 文档从 IO 设备 dev ,返回 true 若内容被成功剖析;否则返回 false .

PySide2.QtXml.QDomDocument. setContent ( dev )
参数

dev QIODevice

返回类型

(retval, errorMsg, errorLine, errorColumn)

这是重载函数。

此函数读取 XML 文档从 IO 设备 dev ,返回 true 若内容被成功剖析;否则返回 false .

不履行名称空间处理。

PySide2.QtXml.QDomDocument. setContent ( reader , namespaceProcessing [ , errorMsg=None [ , errorLine=None [ , errorColumn=None ] ] ] )
参数
  • reader QXmlStreamReader

  • namespaceProcessing bool

  • errorMsg – unicode

  • errorLine int

  • errorColumn int

返回类型

bool

这是重载函数。

此函数读取 XML 文档从 QXmlStreamReader reader 并剖析它。返回 true 若内容被成功解析;否则返回 false .

namespaceProcessing is true ,剖析器识别 XML 文件中的名称空间并将前缀名称、本地名称和名称空间 URI 设为适当值。若 namespaceProcessing is false ,剖析器不处理名称空间当它读取 XML 文件时。

若发生剖析错误,错误消息放置在 * errorMsg ,行号在 * errorLine 和列号在 * errorColumn (除非关联指针被设为 0)。

另请参阅

QXmlStreamReader

PySide2.QtXml.QDomDocument. toByteArray ( [ arg__1=1 ] )
参数

arg__1 int

返回类型

QByteArray

将经剖析文档转换回其正文表示并返回 QByteArray 包含编码为 UTF-8 的数据。

此函数使用 indent 作为缩进子元素的空格数量。

另请参阅

toString()

PySide2.QtXml.QDomDocument. toString ( [ arg__1=1 ] )
参数

arg__1 int

返回类型

unicode

将经剖析文档转换回其正文表示。

此函数使用 indent 作为缩进子元素的空格数量。

indent 为 -1,根本不添加空格。