内容表

上一话题

QDomNamedNodeMap

下一话题

QDomNodeList

QDomNode

QDomNode class is the base class for all the nodes in a DOM tree. 更多

Inheritance diagram of PySide2.QtXml.QDomNode

继承者: QDomAttr , QDomCDATASection , QDomCharacterData , QDomComment , QDomDocument , QDomDocumentFragment , QDomDocumentType , QDomElement , QDomEntity , QDomEntityReference , QDomNotation , QDomProcessingInstruction , QDomText

概要

函数

详细描述

Many functions in the DOM return a QDomNode .

可以找出节点的类型使用 isAttr() , isCDATASection() , isDocumentFragment() , isDocument() , isDocumentType() , isElement() , isEntityReference() , isText() , isEntity() , isNotation() , isProcessingInstruction() , isCharacterData() and isComment() .

A QDomNode can be converted into one of its subclasses using toAttr() , toCDATASection() , toDocumentFragment() , toDocument() , toDocumentType() , toElement() , toEntityReference() , toText() , toEntity() , toNotation() , toProcessingInstruction() , toCharacterData() or toComment() . You can convert a node to a null node with clear() .

Copies of the QDomNode class share their data using explicit sharing. This means that modifying one node will change all copies. This is especially useful in combination with functions which return a QDomNode ,如 firstChild() . You can make an independent (deep) copy of the node with cloneNode() .

A QDomNode can be null, much like None . Creating a copy of a null node results in another null node. It is not possible to modify a null node, but it is possible to assign another, possibly non-null node to it. In this case, the copy of the null node will remain null. You can check if a QDomNode is null by calling isNull() . The empty constructor of a QDomNode (or any of the derived classes) creates a null node.

插入节点采用 insertBefore() , insertAfter() or appendChild() . You can replace one node with another using replaceChild() and remove a node with removeChild() .

要遍历节点使用 firstChild() to get a node’s first child (if any), and nextSibling() to traverse. QDomNode also provides lastChild() , previousSibling() and parentNode() . To find the first child node with a particular node name use namedItem() .

要找出节点是否拥有子级使用 hasChildNodes() and to get a list of all of a node’s children use childNodes() .

The node’s name and value (the meaning of which varies depending on its type) is returned by nodeName() and nodeValue() respectively. The node’s type is returned by nodeType() . The node’s value can be set with setNodeValue() .

节点所属文档的返回通过 ownerDocument() .

相邻 QDomText 节点可以合并成单个节点采用 normalize() .

QDomElement 可以检索节点拥有的属性采用 attributes() .

QDomElement and QDomAttr 节点可以拥有可以检索的名称空间采用 namespaceURI() . Their local name is retrieved with localName() , and their prefix with prefix() . The prefix can be set with setPrefix() .

可以将节点的 XML 表示写入文本流采用 save() .

以下范例查找 XML 文档第一元素并打印其直接子级所有元素的名称。

d = QDomDocument()
d.setContent(someXML)
n = d.firstChild()
while !n.isNull():
    if n.isElement():
        e = n.toElement()
        print "Element name: %s" % e.tagName()
        break
    n = n.nextSibling()
											

有关文档对象模型的进一步信息,见 级别 1 and 级别 2 核心 。有关 DOM 实现的更一般介绍,见 QDomDocument 文档编制。

class QDomNode

QDomNode(arg__1)

param arg__1

QDomNode

构造 null 节点。

构造副本为 n .

拷贝数据是共享的 (浅拷贝):修改一个节点也将改变另一节点。若想要制作深度副本,使用 cloneNode() .

PySide2.QtXml.QDomNode. NodeType

此枚举定义节点的类型:

常量

描述

QDomNode.ElementNode

QDomNode.AttributeNode

QDomNode.TextNode

QDomNode.CDATASectionNode

QDomNode.EntityReferenceNode

QDomNode.EntityNode

QDomNode.ProcessingInstructionNode

QDomNode.CommentNode

QDomNode.DocumentNode

QDomNode.DocumentTypeNode

QDomNode.DocumentFragmentNode

QDomNode.NotationNode

QDomNode.BaseNode

A QDomNode 对象,即不是 QDomNode 子类。

QDomNode.CharacterDataNode

PySide2.QtXml.QDomNode. EncodingPolicy

此枚举指定如何 save() determines what encoding to use when serializing.

常量

描述

QDomNode.EncodingFromDocument

编码抓取自文档。

QDomNode.EncodingFromTextStream

编码抓取自 QTextStream .

另请参阅

save()

PySide2.QtXml.QDomNode. appendChild ( newChild )
参数

newChild QDomNode

返回类型

QDomNode

追加 newChild as the node’s last child.

newChild 是另一节点的子级,它会重设父级到此节点。若 newChild 是此节点的子级,那么它在子级列表中的位置会改变。

newChild QDomDocumentFragment ,那么片段的子级将从片段中移除并追加。

newChild QDomElement 和此节点是 QDomDocument ,已经拥有的元素节点将作为子级, newChild 不添加作为子级并返回 null 节点。

返回新的引用为 newChild 当成功时或 null node 当失败时。

在 null 节点 (例如:采用默认构造函数创建) 调用此函数什么都不做并返回 null node .

DOM 规范禁止插入属性节点,但由于历史原因,无论如何 QDom 接受它们。

PySide2.QtXml.QDomNode. attributes ( )
返回类型

QDomNamedNodeMap

返回所有属性的命名节点映射。才提供属性对于 QDomElement s.

改变映射中的属性也会改变其属性对于此 QDomNode .

PySide2.QtXml.QDomNode. childNodes ( )
返回类型

QDomNodeList

返回所有直接子级节点的列表。

大多数情况下,会调用此函数在 QDomElement 对象。

例如,若 XML 文档看起来像这样:

<body>
<h1>Heading</h1>
<p>Hello <b>you</b></p>
</body>
											

Then the list of child nodes for the “body”-element will contain the node created by the <h1> tag and the node created by the <p> tag.

列表中的节点不是副本;因此改变列表中的节点也会改变此节点的子级。

PySide2.QtXml.QDomNode. clear ( )

将节点转换为 null 节点;若它之前不是 null 节点,删除其类型和内容。

另请参阅

isNull()

PySide2.QtXml.QDomNode. cloneNode ( [ deep=true ] )
参数

deep bool

返回类型

QDomNode

创建深 (非浅) 副本为 QDomNode .

deep is true, then the cloning is done recursively which means that all the node’s children are deep copied too. If deep 为 false 仅节点本身被拷贝且副本没有子级节点。

PySide2.QtXml.QDomNode. columnNumber ( )
返回类型

int

对于创建的节点通过 setContent() , this function returns the column number in the XML document where the node was parsed. Otherwise, -1 is returned.

PySide2.QtXml.QDomNode. firstChild ( )
返回类型

QDomNode

返回节点的第一子级。若没有子级节点, null node 被返回。改变返回节点也会改变文档树节点。

PySide2.QtXml.QDomNode. firstChildElement ( [ tagName="" ] )
参数

tagName – unicode

返回类型

QDomElement

返回的第一子级元素具有标签名称 tagName 若 tagName 非空; 否则返回第一子级元素。返回 null 元素若不存在这样的子级。

PySide2.QtXml.QDomNode. hasAttributes ( )
返回类型

bool

返回 true 若节点拥有属性;否则返回 false .

另请参阅

attributes()

PySide2.QtXml.QDomNode. hasChildNodes ( )
返回类型

bool

返回 true 若节点拥有一个或多个子级;否则返回 false .

PySide2.QtXml.QDomNode. insertAfter ( newChild , refChild )
参数
返回类型

QDomNode

插入节点 newChild 后于子级节点 refChild . refChild 必须是此节点的直接子级。若 refChild is null then newChild is appended as this node’s last child.

newChild 是另一节点的子级,它会重设父级到此节点。若 newChild 是此节点的子级,那么它在子级列表中的位置会改变。

newChild QDomDocumentFragment ,那么片段的子级将从片段被移除并插入后于 refChild .

返回新的引用为 newChild 当成功时或 null node 当失败时。

DOM 规范禁止插入属性节点,但由于历史原因 QDom 仍然接受它们。

PySide2.QtXml.QDomNode. insertBefore ( newChild , refChild )
参数
返回类型

QDomNode

插入节点 newChild 前于子级节点 refChild . refChild 必须是此节点的直接子级。若 refChild is null then newChild is inserted as the node’s first child.

newChild 是另一节点的子级,它会重设父级到此节点。若 newChild 是此节点的子级,那么它在子级列表中的位置会改变。

newChild QDomDocumentFragment ,那么片段的子级将从片段被移除并插入前于 refChild .

返回新的引用为 newChild 当成功时或 null node 当失败时。

DOM 规范禁止插入属性节点,但由于历史原因 QDom 仍然接受它们。

PySide2.QtXml.QDomNode. isAttr ( )
返回类型

bool

返回 true 若节点是属性;否则返回 false .

若此函数返回 true ,它并未暗示此对象是 QDomAttribute;可以采用 toAttribute() 获取 QDomAttribute。

另请参阅

toAttr()

PySide2.QtXml.QDomNode. isCDATASection ( )
返回类型

bool

返回 true 若节点是 CDATA 区间;否则返回 false。

若此函数返回 true ,它并未暗示此对象是 QDomCDATASection ;可以获取 QDomCDATASection with toCDATASection() .

另请参阅

toCDATASection()

PySide2.QtXml.QDomNode. isCharacterData ( )
返回类型

bool

返回 true 若节点是字符数据节点;否则返回 false .

若此函数返回 true ,它并未暗示此对象是 QDomCharacterData ;可以获取 QDomCharacterData with toCharacterData() .

另请参阅

toCharacterData()

PySide2.QtXml.QDomNode. isComment ( )
返回类型

bool

返回 true 若节点是注释;否则返回 false .

若此函数返回 true ,它并未暗示此对象是 QDomComment ;可以获取 QDomComment with toComment() .

另请参阅

toComment()

PySide2.QtXml.QDomNode. isDocument ( )
返回类型

bool

返回 true 若节点是文档;否则返回 false .

若此函数返回 true ,它并未暗示此对象是 QDomDocument ;可以获取 QDomDocument with toDocument() .

另请参阅

toDocument()

PySide2.QtXml.QDomNode. isDocumentFragment ( )
返回类型

bool

返回 true 若节点是文档片段;否则返回 false。

若此函数返回 true ,它并未暗示此对象是 QDomDocumentFragment ;可以获取 QDomDocumentFragment with toDocumentFragment() .

PySide2.QtXml.QDomNode. isDocumentType ( )
返回类型

bool

返回 true 若节点是文档类型;否则返回 false。

若此函数返回 true ,它并未暗示此对象是 QDomDocumentType ;可以获取 QDomDocumentType with toDocumentType() .

另请参阅

toDocumentType()

PySide2.QtXml.QDomNode. isElement ( )
返回类型

bool

返回 true 若节点是元素;否则返回 false .

若此函数返回 true ,它并未暗示此对象是 QDomElement ;可以获取 QDomElement with toElement() .

另请参阅

toElement()

PySide2.QtXml.QDomNode. isEntity ( )
返回类型

bool

返回 true 若节点是实体;否则返回 false .

若此函数返回 true ,它并未暗示此对象是 QDomEntity ;可以获取 QDomEntity with toEntity() .

另请参阅

toEntity()

PySide2.QtXml.QDomNode. isEntityReference ( )
返回类型

bool

返回 true 若节点是实体引用;否则返回 false。

若此函数返回 true ,它并未暗示此对象是 QDomEntityReference ;可以获取 QDomEntityReference with toEntityReference() .

PySide2.QtXml.QDomNode. isNotation ( )
返回类型

bool

返回 true 若节点是表示法;否则返回 false .

若此函数返回 true ,它并未暗示此对象是 QDomNotation ;可以获取 QDomNotation with toNotation() .

另请参阅

toNotation()

PySide2.QtXml.QDomNode. isNull ( )
返回类型

bool

返回 true 若此节点为 null (即:若它没有类型或内容);否则返回 false .

PySide2.QtXml.QDomNode. isProcessingInstruction ( )
返回类型

bool

返回 true 若节点是处理指令;否则返回 false .

若此函数返回 true ,它并未暗示此对象是 QDomProcessingInstruction ;可以获取 QProcessingInstruction 采用 toProcessingInstruction() .

PySide2.QtXml.QDomNode. isSupported ( feature , version )
参数
  • feature – unicode

  • version – unicode

返回类型

bool

返回 true 若 DOM (文档对象模型) 实现实现特征 feature 且此特征由此节点支持在版本 version ;否则返回 false .

另请参阅

hasFeature()

PySide2.QtXml.QDomNode. isText ( )
返回类型

bool

返回 true 若节点是文本节点;否则返回 false .

若此函数返回 true ,它并未暗示此对象是 QDomText ;可以获取 QDomText with toText() .

另请参阅

toText()

PySide2.QtXml.QDomNode. lastChild ( )
返回类型

QDomNode

返回节点的最后子级。若没有子级节点, null node 被返回。改变返回节点也会改变文档树节点。

PySide2.QtXml.QDomNode. lastChildElement ( [ tagName="" ] )
参数

tagName – unicode

返回类型

QDomElement

返回的最后子级元素具有标签名称 tagName 若 tagName 非空;否则返回最后子级元素。返回 null 元素,若不存在这种子级。

PySide2.QtXml.QDomNode. lineNumber ( )
返回类型

int

对于创建的节点通过 setContent() , this function returns the line number in the XML document where the node was parsed. Otherwise, -1 is returned.

PySide2.QtXml.QDomNode. localName ( )
返回类型

unicode

若节点使用名称空间,此函数返回节点的本地名称;否则它返回空字符串。

仅节点为类型 ElementNode or AttributeNode 可以拥有名称空间。名称空间必须在创建时指定;之后添加名称空间不可能。

PySide2.QtXml.QDomNode. namedItem ( name )
参数

name – unicode

返回类型

QDomNode

返回第一直接子级节点对于其 nodeName() 等于 name .

若不存在这种直接子级, null node 被返回。

另请参阅

nodeName()

PySide2.QtXml.QDomNode. namespaceURI ( )
返回类型

unicode

返回此节点的名称空间 URI 或空字符串,若节点没有名称空间 URI。

仅节点为类型 ElementNode or AttributeNode 可以拥有名称空间。名称空间 URI 必须在创建时指定且以后不能更改。

PySide2.QtXml.QDomNode. nextSibling ( )
返回类型

QDomNode

返回文档树中的下一同级。改变返回节点还会改变文档树中的节点。

若拥有的 XML 像这样:

<h1>Heading</h1>
<p>The text...</p>
<h2>Next heading</h2>
											

和此 QDomNode represents the <p> tag, will return the node representing the <h2> tag.

另请参阅

previousSibling()

PySide2.QtXml.QDomNode. nextSiblingElement ( [ taName="" ] )
参数

taName – unicode

返回类型

QDomElement

返回的下一同级元素具有标签名称 tagName if tagName 非空;否则返回任何下一同级元素。返回 null 元素,若不存在这种同级。

PySide2.QtXml.QDomNode. nodeName ( )
返回类型

unicode

返回节点的名称。

名称的含义从属子类:

Name

含义

QDomAttr

属性名称

QDomCDATASection

The string “#cdata-section”

QDomComment

The string “#comment”

QDomDocument

The string “#document”

QDomDocumentFragment

The string “#document-fragment”

QDomDocumentType

文件类型的名称

QDomElement

标签名称

QDomEntity

实体名称

QDomEntityReference

引用实体的名称

QDomNotation

表示法名称

QDomProcessingInstruction

处理指令的目标

QDomText

The string “#text”

注意

This function does not take the presence of namespaces into account when processing the names of element and attribute nodes. As a result, the returned name can contain any namespace prefix that may be present. To obtain the node name of an element or attribute, use localName() ; to obtain the namespace prefix, use namespaceURI() .

另请参阅

nodeValue()

PySide2.QtXml.QDomNode. nodeType ( )
返回类型

NodeType

返回节点的类型。

PySide2.QtXml.QDomNode. nodeValue ( )
返回类型

unicode

返回节点的值。

值的含义从属子类:

Name

含义

QDomAttr

属性值

QDomCDATASection

CDATA 区间的内容

QDomComment

注释

QDomProcessingInstruction

处理指令的数据

QDomText

文本

All the other subclasses do not have a node value and will return an empty string.

PySide2.QtXml.QDomNode. normalize ( )

Calling on an element converts all its children into a standard form. This means that adjacent QDomText objects will be merged into a single text object ( QDomCDATASection nodes are not merged).

PySide2.QtXml.QDomNode. __ne__ ( arg__1 )
参数

arg__1 QDomNode

返回类型

bool

返回 true if n 和此 DOM 节点不相等;否则返回 false .

PySide2.QtXml.QDomNode. __eq__ ( arg__1 )
参数

arg__1 QDomNode

返回类型

bool

返回 true if n 和此 DOM 节点相等;否则返回 false .

Any instance of QDomNode acts as a reference to an underlying data structure in QDomDocument . The test for equality checks if the two references point to the same underlying node. For example:

QDomDocument document
QDomElement element1 = document.documentElement()
QDomElement element2 = element1
											

2 节点 ( QDomElement QDomNode subclass) both refer to the document’s root element, and element1 == element2 will return true. On the other hand:

QDomElement element3 = document.createElement("MyElement")
QDomElement element4 = document.createElement("MyElement")
											

Even though both nodes are empty elements carrying the same name, element3 == element4 will return false because they refer to two different nodes in the underlying data structure.

PySide2.QtXml.QDomNode. ownerDocument ( )
返回类型

QDomDocument

返回此节点所属的文档。

PySide2.QtXml.QDomNode. parentNode ( )
返回类型

QDomNode

返回父级节点。若此节点没有父级,返回 null 节点 (即:节点的 isNull() 返回 true ).

PySide2.QtXml.QDomNode. prefix ( )
返回类型

unicode

返回节点名称空间前缀,或空字符串若节点没有名称空间前缀。

仅节点为类型 ElementNode or AttributeNode 可以拥有名称空间。必须在创建时指定名称空间前缀。若节点是采用名称空间前缀创建的,可以稍后改变它采用 setPrefix() .

如果创建元素或属性采用 createElement() or createAttribute() , the prefix will be an empty string. If you use createElementNS() or createAttributeNS() instead, the prefix will not be an empty string; but it might be an empty string if the name does not have a prefix.

PySide2.QtXml.QDomNode. previousSibling ( )
返回类型

QDomNode

返回文档树的上一同级。改变返回节点也会改变文档树节点。

例如,若拥有的 XML 像这样:

<h1>Heading</h1>
<p>The text...</p>
<h2>Next heading</h2>
											

和此 QDomNode represents the <p> tag, will return the node representing the <h1> tag.

另请参阅

nextSibling()

PySide2.QtXml.QDomNode. previousSiblingElement ( [ tagName="" ] )
参数

tagName – unicode

返回类型

QDomElement

返回的上一同级元素具有标签名称 tagName if tagName 非空;否则返回任何先前同级元素。返回 null 元素若不存在这样的同级。

PySide2.QtXml.QDomNode. removeChild ( oldChild )
参数

oldChild QDomNode

返回类型

QDomNode

移除 oldChild from the list of children. oldChild 必须是此节点的直接子级。

返回新的引用为 oldChild 当成功时或 null node 当失败时。

PySide2.QtXml.QDomNode. replaceChild ( newChild , oldChild )
参数
返回类型

QDomNode

替换 oldChild with newChild . oldChild 必须是此节点的直接子级。

newChild 是另一节点的子级,它会重设父级到此节点。若 newChild 是此节点的子级,那么它在子级列表中的位置会改变。

newChild QDomDocumentFragment ,那么 oldChild 被片段的所有子级所替换。

返回新的引用为 oldChild 当成功时或 null node 当失败时。

PySide2.QtXml.QDomNode. save ( arg__1 , arg__2 [ , arg__3=QDomNode.EncodingFromDocument ] )
参数

将节点及其所有子级的 XML 表示写入流 stream 。此函数使用 indent 作为节点的缩进空格数。

若文档包含无效 XML 字符或不能以给定编码编码字符,结果和行为未定义。

encodingPolicy is EncodingFromDocument 和此节点是文档节点,编码的文本流 stream ‘s encoding is set by treating a processing instruction by name “xml” as an XML declaration, if one exists, and otherwise defaults to UTF-8. XML declarations are not processing instructions, but this behavior exists for historical reasons. If this node is not a document node, the text stream’s encoding is used.

encodingPolicy is EncodingFromTextStream 和此节点是文档节点,此函数行为如 save( QTextStream &str, int indent) 除了指定编码在文本流 stream 被使用。

若文档包含无效 XML 字符或不能以给定编码编码字符,结果和行为未定义。

PySide2.QtXml.QDomNode. setNodeValue ( arg__1 )
参数

arg__1 – unicode

Sets the node’s value to v .

另请参阅

nodeValue()

PySide2.QtXml.QDomNode. setPrefix ( pre )
参数

pre – unicode

若节点拥有名称空间前缀,此函数将节点名称空间前缀改为 pre 。否则此函数什么都不做。

仅节点为类型 ElementNode or AttributeNode 可以拥有名称空间。必须在创建时指定名称空间前缀;之后添加名称空间前缀是不可能的。

PySide2.QtXml.QDomNode. toAttr ( )
返回类型

QDomAttr

转换 QDomNode QDomAttr 。若节点不是属性,返回对象将是 null .

另请参阅

isAttr()

PySide2.QtXml.QDomNode. toCDATASection ( )
返回类型

QDomCDATASection

转换 QDomNode QDomCDATASection 。若节点不是 CDATA 区间,返回对象将是 null .

另请参阅

isCDATASection()

PySide2.QtXml.QDomNode. toCharacterData ( )
返回类型

QDomCharacterData

转换 QDomNode QDomCharacterData 。若节点不是字符数据,返回对象将是 null .

另请参阅

isCharacterData()

PySide2.QtXml.QDomNode. toComment ( )
返回类型

QDomComment

转换 QDomNode QDomComment 。若节点不是注释,返回对象将是 null .

另请参阅

isComment()

PySide2.QtXml.QDomNode. toDocument ( )
返回类型

QDomDocument

转换 QDomNode QDomDocument 。若节点不是文档,返回对象将是 null .

另请参阅

isDocument()

PySide2.QtXml.QDomNode. toDocumentFragment ( )
返回类型

QDomDocumentFragment

转换 QDomNode QDomDocumentFragment 。若节点不是文档片段,返回对象将是 null .

PySide2.QtXml.QDomNode. toDocumentType ( )
返回类型

QDomDocumentType

转换 QDomNode QDomDocumentType 。若节点不是文档类型,返回对象将是 null .

另请参阅

isDocumentType()

PySide2.QtXml.QDomNode. toElement ( )
返回类型

QDomElement

转换 QDomNode QDomElement 。若节点不是元素,返回对象将是 null .

另请参阅

isElement()

PySide2.QtXml.QDomNode. toEntity ( )
返回类型

QDomEntity

转换 QDomNode QDomEntity 。若节点不是实体,返回对象将是 null .

另请参阅

isEntity()

PySide2.QtXml.QDomNode. toEntityReference ( )
返回类型

QDomEntityReference

转换 QDomNode QDomEntityReference 。若节点不是实体引用,返回对象将是 null .

PySide2.QtXml.QDomNode. toNotation ( )
返回类型

QDomNotation

转换 QDomNode QDomNotation 。若节点不是表示法,返回对象将是 null .

另请参阅

isNotation()

PySide2.QtXml.QDomNode. toProcessingInstruction ( )
返回类型

QDomProcessingInstruction

转换 QDomNode QDomProcessingInstruction 。若节点不是处理指令,返回对象将是 null .

PySide2.QtXml.QDomNode. toText ( )
返回类型

QDomText

转换 QDomNode QDomText 。若节点不是文本,返回对象将是 null .

另请参阅

isText()