QDomElement¶
The
QDomElementclass represents one element in the DOM tree. More…

Synopsis¶
Functions¶
def
attribute(name[, defValue=””])def
attributeNS(nsURI, localName[, defValue=””])def
attributeNode(name)def
attributeNodeNS(nsURI, localName)def
elementsByTagName(tagname)def
elementsByTagNameNS(nsURI, localName)def
hasAttribute(name)def
hasAttributeNS(nsURI, localName)def
removeAttribute(name)def
removeAttributeNS(nsURI, localName)def
removeAttributeNode(oldAttr)def
setAttribute(name, value)def
setAttribute(name, value)def
setAttribute(name, value)def
setAttributeNS(nsURI, qName, value)def
setAttributeNS(nsURI, qName, value)def
setAttributeNS(nsURI, qName, value)def
setAttributeNS(nsURI, qName, value)def
setAttributeNS(nsURI, qName, value)def
setAttributeNS(nsURI, qName, value)def
setAttributeNode(newAttr)def
setAttributeNodeNS(newAttr)def
setTagName(name)def
tagName()def
text()
Detailed Description¶
Elements have a
tagName()and zero or more attributes associated with them. The tag name can be changed withsetTagName().Element attributes are represented by
QDomAttrobjects that can be queried using theattribute()andattributeNode()functions. You can set attributes with thesetAttribute()andsetAttributeNode()functions. Attributes can be removed withremoveAttribute(). There are namespace-aware equivalents to these functions, i.e.setAttributeNS(),setAttributeNodeNS()andremoveAttributeNS().If you want to access the text of a node use
text(), e.g.e = # some QDomElement... #... s = e.text()The
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 forQDomTextnodes, 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 usingfirstChild().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()orelementsByTagNameNS().To browse the elements of a dom document use
firstChildElement(),lastChildElement(),nextSiblingElement()andpreviousSiblingElement(). 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
QDomDocumentdocumentation.
- class PySide2.QtXml.QDomElement¶
PySide2.QtXml.QDomElement(x)
- param x:
Constructs an empty element. Use the
createElement()function to construct elements with content.
- PySide2.QtXml.QDomElement.attribute(name[, defValue=""])¶
- Parameters:
name – str
defValue – str
- Return type:
str
Returns the attribute called
name. If the attribute does not existdefValueis returned.
- PySide2.QtXml.QDomElement.attributeNS(nsURI, localName[, defValue=""])¶
- Parameters:
nsURI – str
localName – str
defValue – str
- Return type:
str
Returns the attribute with the local name
localNameand the namespace URInsURI. If the attribute does not existdefValueis returned.
- PySide2.QtXml.QDomElement.attributeNode(name)¶
- Parameters:
name – str
- Return type:
Returns the
QDomAttrobject that corresponds to the attribute calledname. If no such attribute exists anull attributeis returned.
- PySide2.QtXml.QDomElement.attributeNodeNS(nsURI, localName)¶
- Parameters:
nsURI – str
localName – str
- Return type:
Returns the
QDomAttrobject that corresponds to the attribute with the local namelocalNameand the namespace URInsURI. If no such attribute exists anull attributeis returned.
- PySide2.QtXml.QDomElement.elementsByTagName(tagname)¶
- Parameters:
tagname – str
- Return type:
Returns a
QDomNodeListcontaining all descendants of this element namedtagnameencountered during a preorder traversal of the element subtree with this element as its root. The order of the elements in the returned list is the order they are encountered during the preorder traversal.See also
- PySide2.QtXml.QDomElement.elementsByTagNameNS(nsURI, localName)¶
- Parameters:
nsURI – str
localName – str
- Return type:
Returns a
QDomNodeListcontaining all descendants of this element with local namelocalNameand namespace URInsURIencountered during a preorder traversal of the element subtree with this element as its root. The order of the elements in the returned list is the order they are encountered during the preorder traversal.See also
- PySide2.QtXml.QDomElement.hasAttribute(name)¶
- Parameters:
name – str
- Return type:
bool
Returns
trueif this element has an attribute calledname; otherwise returnsfalse.Note
This function does not take the presence of namespaces into account. As a result, the specified name will be tested against fully-qualified attribute names that include any namespace prefixes that may be present.
Use
hasAttributeNS()to explicitly test for attributes with specific namespaces and names.
- PySide2.QtXml.QDomElement.hasAttributeNS(nsURI, localName)¶
- Parameters:
nsURI – str
localName – str
- Return type:
bool
Returns
trueif this element has an attribute with the local namelocalNameand the namespace URInsURI; otherwise returns false.
- PySide2.QtXml.QDomElement.removeAttribute(name)¶
- Parameters:
name – str
Removes the attribute called name
namefrom this element.See also
- PySide2.QtXml.QDomElement.removeAttributeNS(nsURI, localName)¶
- Parameters:
nsURI – str
localName – str
Removes the attribute with the local name
localNameand the namespace URInsURIfrom this element.
- PySide2.QtXml.QDomElement.removeAttributeNode(oldAttr)¶
- Parameters:
oldAttr –
PySide2.QtXml.QDomAttr- Return type:
Removes the attribute
oldAttrfrom the element and returns it.See also
- PySide2.QtXml.QDomElement.setAttribute(name, value)¶
- Parameters:
name – str
value – str
- PySide2.QtXml.QDomElement.setAttribute(name, value)
- Parameters:
name – str
value –
double
- PySide2.QtXml.QDomElement.setAttribute(name, value)
- Parameters:
name – str
value –
qlonglong
- PySide2.QtXml.QDomElement.setAttributeNS(nsURI, qName, value)¶
- Parameters:
nsURI – str
qName – str
value –
uint
- PySide2.QtXml.QDomElement.setAttributeNS(nsURI, qName, value)
- Parameters:
nsURI – str
qName – str
value –
qulonglong
- PySide2.QtXml.QDomElement.setAttributeNS(nsURI, qName, value)
- Parameters:
nsURI – str
qName – str
value – int
- PySide2.QtXml.QDomElement.setAttributeNS(nsURI, qName, value)
- Parameters:
nsURI – str
qName – str
value –
double
- PySide2.QtXml.QDomElement.setAttributeNS(nsURI, qName, value)
- Parameters:
nsURI – str
qName – str
value – str
- PySide2.QtXml.QDomElement.setAttributeNS(nsURI, qName, value)
- Parameters:
nsURI – str
qName – str
value –
qlonglong
- PySide2.QtXml.QDomElement.setAttributeNode(newAttr)¶
- Parameters:
newAttr –
PySide2.QtXml.QDomAttr- Return type:
Adds the attribute
newAttrto this element.If the element has another attribute that has the same name as
newAttr, this function replaces that attribute and returns it; otherwise the function returns anull attribute.
- PySide2.QtXml.QDomElement.setAttributeNodeNS(newAttr)¶
- Parameters:
newAttr –
PySide2.QtXml.QDomAttr- Return type:
Adds the attribute
newAttrto this element.If the element has another attribute that has the same local name and namespace URI as
newAttr, this function replaces that attribute and returns it; otherwise the function returns anull attribute.
- PySide2.QtXml.QDomElement.setTagName(name)¶
- Parameters:
name – str
Sets this element’s tag name to
name.See also
- PySide2.QtXml.QDomElement.tagName()¶
- Return type:
str
Returns the tag name of this element. For an XML element like this:
<img src="myimg.png">
the tagname would return “img”.
See also
- PySide2.QtXml.QDomElement.text()¶
- Return type:
str
Returns the element’s text or an empty string.
Example:
<h1>Hello <b>Qt</b> <![CDATA[<xml is cool>]]></h1>
The function of the
QDomElementfor the<h1>tag, will return the following text:Hello Qt <xml is cool>
Comments are ignored by this function. It only evaluates
QDomTextandQDomCDATASectionobjects.
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.