PySide.QtXml.QDomElement class represents one element in the DOM tree.
元素拥有 PySide.QtXml.QDomElement.tagName() and zero or more attributes associated with them. The tag name can be changed with PySide.QtXml.QDomElement.setTagName() .
元素属性的表示通过 PySide.QtXml.QDomAttr 对象,可以查询使用 PySide.QtXml.QDomElement.attribute() and PySide.QtXml.QDomElement.attributeNode() functions. You can set attributes with the PySide.QtXml.QDomElement.setAttribute() and PySide.QtXml.QDomElement.setAttributeNode() functions. Attributes can be removed with PySide.QtXml.QDomElement.removeAttribute() . There are namespace-aware equivalents to these functions, i.e. PySide.QtXml.QDomElement.setAttributeNS() , PySide.QtXml.QDomElement.setAttributeNodeNS() and PySide.QtXml.QDomElement.removeAttributeNS() .
若想要访问节点文本,使用 PySide.QtXml.QDomElement.text() ,如
e = # some QDomElement...
#...
s = e.text()
PySide.QtXml.QDomElement.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 PySide.QtXml.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 PySide.QtXml.QDomElement.text() rather than using PySide.QtXml.QDomNode.firstChild() . PySide.QtXml.QDomNode.toText() . data() or n. PySide.QtXml.QDomNode.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 PySide.QtXml.QDomElement.elementsByTagName() or PySide.QtXml.QDomElement.elementsByTagNameNS() .
To browse the elements of a dom document use PySide.QtXml.QDomNode.firstChildElement() , PySide.QtXml.QDomNode.lastChildElement() , PySide.QtXml.QDomNode.nextSiblingElement() and PySide.QtXml.QDomNode.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")
For further information about the Document Object Model see Level 1 and Level 2 Core. For a more general introduction of the DOM implementation see the PySide.QtXml.QDomDocument 文档编制。
| 参数: | x – PySide.QtXml.QDomElement |
|---|
Constructs an empty element. Use the QDomDocument.createElement() function to construct elements with content.
构造副本为 x .
拷贝数据是共享的 (浅拷贝):修改一个节点也将改变另一节点。若想要制作深度副本,使用 PySide.QtXml.QDomNode.cloneNode() .
| 参数: |
|
|---|---|
| 返回类型: |
unicode |
返回的属性称为 name 。若属性不存在 defValue 被返回。
| 参数: |
|
|---|---|
| 返回类型: |
unicode |
返回的属性具有本地名称 localName 和名称空间 URI nsURI 。若属性不存在 defValue 被返回。
| 参数: | name – unicode |
|---|---|
| 返回类型: | PySide.QtXml.QDomAttr |
返回 PySide.QtXml.QDomAttr 对象相当于属性称为 name 。若不存在这种属性 null attribute 被返回。
| 参数: |
|
|---|---|
| 返回类型: |
返回 PySide.QtXml.QDomAttr 对象相当于属性带有本地名称 localName 和名称空间 URI nsURI 。若不存在这种属性 null attribute 被返回。
| 参数: | tagname – unicode |
|---|---|
| 返回类型: | PySide.QtXml.QDomNodeList |
返回 PySide.QtXml.QDomNodeList 包含此元素的所有后代名为 tagname 在采用此元素作为其根的元素子树的预顺序遍历期间所遇到的。返回列表中的元素次序是在预顺序遍历期间遇到它们的次序。
| 参数: |
|
|---|---|
| 返回类型: |
返回 PySide.QtXml.QDomNodeList 包含此元素的所有后代具有本地名称 localName and namespace URI nsURI 在采用此元素作为其根的元素子树的预顺序遍历期间所遇到的。返回列表中的元素次序是在预顺序遍历期间遇到它们的次序。
| 参数: | name – unicode |
|---|---|
| 返回类型: | PySide.QtCore.bool |
Returns true if this element has an attribute called name ;否则返回 false。
注意
此函数不考虑名称空间的存在。因此,将测试指定名称 (针对包括可能存在的任何名称空间前缀的完全限定属性名)。
使用 PySide.QtXml.QDomElement.hasAttributeNS() to explicitly test for attributes with specific namespaces and names.
| 参数: |
|
|---|---|
| 返回类型: |
PySide.QtCore.bool |
Returns true if this element has an attribute with the local name localName 和名称空间 URI nsURI ;否则返回 false。
| 参数: | name – unicode |
|---|
移除的属性名为 name 从此元素。
| 参数: |
|
|---|
移除属性具有本地名称 localName 和名称空间 URI nsURI 从此元素。
| 参数: | oldAttr – PySide.QtXml.QDomAttr |
|---|---|
| 返回类型: | PySide.QtXml.QDomAttr |
移除属性 oldAttr 从元素并返回它。
| 参数: |
|
|---|
这是重载函数。
The number is formatted according to the current locale.
| 参数: |
|
|---|
这是重载函数。
The number is formatted according to the current locale.
| 参数: |
|
|---|
这是重载函数。
The number is formatted according to the current locale.
| 参数: |
|
|---|
这是重载函数。
The number is formatted according to the current locale.
| 参数: |
|
|---|
这是重载函数。
The number is formatted according to the current locale.
| 参数: |
|
|---|
添加的属性称为 name 采用值 value 。若存在同名属性,其值被替换由 value .
| 参数: |
|
|---|
这是重载函数。
The number is formatted according to the current locale.
| 参数: |
|
|---|
这是重载函数。
| 参数: |
|
|---|
这是重载函数。
| 参数: |
|
|---|
这是重载函数。
| 参数: |
|
|---|
这是重载函数。
| 参数: |
|
|---|
这是重载函数。
| 参数: |
|
|---|
Adds an attribute with the qualified name qName 和名称空间 URI nsURI 采用值 value . If an attribute with the same local name and namespace URI exists, its prefix is replaced by the prefix of qName and its value is repaced by value .
尽管 qName is the qualified name, the local name is used to decide if an existing attribute's value should be replaced.
| 参数: | newAttr – PySide.QtXml.QDomAttr |
|---|---|
| 返回类型: | PySide.QtXml.QDomAttr |
添加属性 newAttr 到此元素。
若拥有另一属性的元素拥有相同名称如 newAttr ,此函数替换该属性并返回它;否则函数返回 null attribute .
| 参数: | newAttr – PySide.QtXml.QDomAttr |
|---|---|
| 返回类型: | PySide.QtXml.QDomAttr |
添加属性 newAttr 到此元素。
如果拥有另一属性的元素拥有相同的本地名称和名称空间 URI 如 newAttr ,此函数替换该属性并返回它;否则函数返回 null attribute .
| 参数: | name – unicode |
|---|
将此元素的标签名称设为 name .
| 返回类型: | unicode |
|---|
返回此元素的标签名称。对于 XML 元素像这样:
<img src="myimg.png">
the tagname would return “img”.
| 返回类型: | unicode |
|---|
返回元素的文本或空字符串。
范例:
<h1>Hello <b>Qt</b> <![CDATA[<xml is cool>]]></h1>
函数 PySide.QtXml.QDomElement.text() 的 PySide.QtXml.QDomElement 为 <h1> 标签,将返回以下文本:
Hello Qt <xml is cool>
此函数忽略注释。它只评估 PySide.QtXml.QDomText and PySide.QtXml.QDomCDATASection 对象。