内容表

上一话题

QDomDocumentType

下一话题

QDomEntity

QDomElement

QDomElement class represents one element in the DOM tree. 更多

Inheritance diagram of PySide2.QtXml.QDomElement

概要

函数

详细描述

元素拥有 tagName() and zero or more attributes associated with them. The tag name can be changed with setTagName() .

元素属性的表示通过 QDomAttr 对象,可以查询使用 attribute() and attributeNode() functions. You can set attributes with the setAttribute() and setAttributeNode() functions. Attributes can be removed with removeAttribute() . There are namespace-aware equivalents to these functions, i.e. setAttributeNS() , setAttributeNodeNS() and removeAttributeNS() .

若想要访问节点文本,使用 text() ,如

e = # some QDomElement...
#...
s = e.text()
											

text() function operates recursively to find the text (since not all elements contain text). If you want to find all the text in all of a node’s children, iterate over the children looking for QDomText nodes, e.g.

text = QString()
element = doc.documentElement()
n = element.firstChild()
while True:
    if not n.isNull()
        break
    t = n.toText()
    if !t.isNull():
        text += t.data()
    n = n.nextSibling()
											

Note that we attempt to convert each node to a text node and use text() rather than using firstChild() . toText() . data() or n. toText() . data() directly on the node, because the node may not be a text element.

You can get a list of all the decendents of an element which have a specified tag name with elementsByTagName() or elementsByTagNameNS() .

To browse the elements of a dom document use firstChildElement() , lastChildElement() , nextSiblingElement() and previousSiblingElement() . For example, to iterate over all child elements called “entry” in a root element called “database”, you can use:

doc = # some QDomDocument ...
root = doc.firstChildElement("database")
elt = root.firstChildElement("entry")
while True:
    if not elt.isNull():
        break
    # ...
    elt = elt.nextSiblingElement("entry")
											

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

class QDomElement

QDomElement(x)

param x

QDomElement

Constructs an empty element. Use the createElement() function to construct elements with content.

PySide2.QtXml.QDomElement. attribute ( name [ , defValue="" ] )
参数
  • name – unicode

  • defValue – unicode

返回类型

unicode

返回的属性称为 name 。若属性不存在 defValue 被返回。

PySide2.QtXml.QDomElement. attributeNS ( nsURI , localName [ , defValue="" ] )
参数
  • nsURI – unicode

  • localName – unicode

  • defValue – unicode

返回类型

unicode

返回的属性具有本地名称 localName 和名称空间 URI nsURI 。若属性不存在 defValue 被返回。

PySide2.QtXml.QDomElement. attributeNode ( name )
参数

name – unicode

返回类型

QDomAttr

返回 QDomAttr 对象相当于属性称为 name 。若不存在这种属性 null attribute 被返回。

PySide2.QtXml.QDomElement. attributeNodeNS ( nsURI , localName )
参数
  • nsURI – unicode

  • localName – unicode

返回类型

QDomAttr

返回 QDomAttr 对象相当于属性带有本地名称 localName 和名称空间 URI nsURI 。若不存在这种属性 null attribute 被返回。

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

tagname – unicode

返回类型

QDomNodeList

返回 QDomNodeList 包含此元素的所有后代名为 tagname 在采用此元素作为其根的元素子树的预顺序遍历期间所遇到的。返回列表中的元素次序是在预顺序遍历期间遇到它们的次序。

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

  • localName – unicode

返回类型

QDomNodeList

返回 QDomNodeList 包含此元素的所有后代具有本地名称 localName and namespace URI nsURI 在采用此元素作为其根的元素子树的预顺序遍历期间所遇到的。返回列表中的元素次序是在预顺序遍历期间遇到它们的次序。

PySide2.QtXml.QDomElement. hasAttribute ( name )
参数

name – unicode

返回类型

bool

返回 true 若此元素拥有的属性称为 name ;否则返回 false .

注意

此函数不考虑名称空间的存在。因此,将测试指定名称 (针对包括可能存在的任何名称空间前缀的完全限定属性名)。

使用 hasAttributeNS() to explicitly test for attributes with specific namespaces and names.

PySide2.QtXml.QDomElement. hasAttributeNS ( nsURI , localName )
参数
  • nsURI – unicode

  • localName – unicode

返回类型

bool

返回 true 若此元素拥有的属性具有本地名称 localName 和名称空间 URI nsURI ;否则返回 false。

PySide2.QtXml.QDomElement. removeAttribute ( name )
参数

name – unicode

移除的属性名为 name 从此元素。

PySide2.QtXml.QDomElement. removeAttributeNS ( nsURI , localName )
参数
  • nsURI – unicode

  • localName – unicode

移除属性具有本地名称 localName 和名称空间 URI nsURI 从此元素。

PySide2.QtXml.QDomElement. removeAttributeNode ( oldAttr )
参数

oldAttr QDomAttr

返回类型

QDomAttr

移除属性 oldAttr 从元素并返回它。

PySide2.QtXml.QDomElement. setAttribute ( name , value )
参数
  • name – unicode

  • value – unicode

PySide2.QtXml.QDomElement. setAttribute ( name , value )
参数
  • name – unicode

  • value double

PySide2.QtXml.QDomElement. setAttribute ( name , value )
参数
  • name – unicode

  • value float

PySide2.QtXml.QDomElement. setAttribute ( name , value )
参数
  • name – unicode

  • value int

PySide2.QtXml.QDomElement. setAttribute ( name , value )
参数
  • name – unicode

  • value qlonglong

PySide2.QtXml.QDomElement. setAttribute ( name , value )
参数
  • name – unicode

  • value qulonglong

PySide2.QtXml.QDomElement. setAttribute ( name , value )
参数
  • name – unicode

  • value uint

PySide2.QtXml.QDomElement. setAttributeNS ( nsURI , qName , value )
参数
  • nsURI – unicode

  • qName – unicode

  • value uint

PySide2.QtXml.QDomElement. setAttributeNS ( nsURI , qName , value )
参数
  • nsURI – unicode

  • qName – unicode

  • value qulonglong

PySide2.QtXml.QDomElement. setAttributeNS ( nsURI , qName , value )
参数
  • nsURI – unicode

  • qName – unicode

  • value int

PySide2.QtXml.QDomElement. setAttributeNS ( nsURI , qName , value )
参数
  • nsURI – unicode

  • qName – unicode

  • value double

PySide2.QtXml.QDomElement. setAttributeNS ( nsURI , qName , value )
参数
  • nsURI – unicode

  • qName – unicode

  • value – unicode

PySide2.QtXml.QDomElement. setAttributeNS ( nsURI , qName , value )
参数
  • nsURI – unicode

  • qName – unicode

  • value qlonglong

PySide2.QtXml.QDomElement. setAttributeNode ( newAttr )
参数

newAttr QDomAttr

返回类型

QDomAttr

添加属性 newAttr 到此元素。

若拥有另一属性的元素拥有相同名称如 newAttr ,此函数替换该属性并返回它;否则函数返回 null attribute .

PySide2.QtXml.QDomElement. setAttributeNodeNS ( newAttr )
参数

newAttr QDomAttr

返回类型

QDomAttr

添加属性 newAttr 到此元素。

如果拥有另一属性的元素拥有相同的本地名称和名称空间 URI 如 newAttr ,此函数替换该属性并返回它;否则函数返回 null attribute .

PySide2.QtXml.QDomElement. setTagName ( name )
参数

name – unicode

Sets this element’s tag name to name .

另请参阅

tagName()

PySide2.QtXml.QDomElement. tagName ( )
返回类型

unicode

返回此元素的标签名称。对于 XML 元素像这样:

<img src="myimg.png">
											

the tagname would return “img”.

另请参阅

setTagName()

PySide2.QtXml.QDomElement. text ( )
返回类型

unicode

Returns the element’s text or an empty string.

范例:

<h1>Hello <b>Qt</b> <![CDATA[<xml is cool>]]></h1>
											

The function of the QDomElement <h1> 标签,将返回以下文本:

Hello Qt <xml is cool>
											

此函数忽略注释。它只评估 QDomText and QDomCDATASection 对象。