PySide6.QtXml.QDomElement¶
- class QDomElement¶
The
QDomElementclass represents one element in the DOM tree. More…Synopsis¶
Methods¶
def
__init__()def
attribute()def
attributeNS()def
attributeNode()def
hasAttribute()def
hasAttributeNS()def
setAttribute()def
setAttributeNS()def
setTagName()def
tagName()def
text()
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
Detailed Description¶
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
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 = //... #... 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() for(QDomNode n = element.firstChild(); not n.isNull(); n = n.nextSibling()) t = n.toText() if not t.isNull(): text += t.data()
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 descendents 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 = // ... root = doc.firstChildElement("database") elt = root.firstChildElement("entry") for (; not elt.isNull(); 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.- __init__()¶
Constructs an empty element. Use the
createElement()function to construct elements with content.- __init__(element)
- Parameters:
element –
QDomElement
Constructs a copy of
element.The data of the copy is shared (shallow copy): modifying one node will also change the other. If you want to make a deep copy, use
cloneNode().- attribute(name[, defValue=""])¶
- Parameters:
name – str
defValue – str
- Return type:
str
Returns the attribute called
name. If the attribute does not existdefValueis returned.- 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.Returns the
QDomAttrobject that corresponds to the attribute calledname. If no such attribute exists anull attributeis returned.Returns the
QDomAttrobject that corresponds to the attribute with the local namelocalNameand the namespace URInsURI. If no such attribute exists anull attributeis returned.- 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
- 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
- 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.- 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.- removeAttribute(name)¶
- Parameters:
name – str
Removes the attribute called name
namefrom this element.See also
- removeAttributeNS(nsURI, localName)¶
- Parameters:
nsURI – str
localName – str
Removes the attribute with the local name
localNameand the namespace URInsURIfrom this element.Removes the attribute
oldAttrfrom the element and returns it.See also
- setAttribute(name, value)¶
- Parameters:
name – str
value – str
Adds an attribute called
namewith valuevalue. If an attribute with the same name exists, its value is replaced byvalue.- setAttribute(name, value)
- Parameters:
name – str
value – float
This is an overloaded function.
The formatting always uses QLocale::C.
- setAttribute(name, value)
- Parameters:
name – str
value – int
This is an overloaded function.
The formatting always uses QLocale::C.
- setAttributeNS(nsURI, qName, value)¶
- Parameters:
nsURI – str
qName – str
value – str
Adds an attribute with the qualified name
qNameand the namespace URInsURIwith the valuevalue. If an attribute with the same local name and namespace URI exists, its prefix is replaced by the prefix ofqNameand its value is replaced byvalue.Although
qNameis the qualified name, the local name is used to decide if an existing attribute’s value should be replaced.- setAttributeNS(nsURI, qName, value)
- Parameters:
nsURI – str
qName – str
value – float
This is an overloaded function.
- setAttributeNS(nsURI, qName, value)
- Parameters:
nsURI – str
qName – str
value – int
This is an overloaded function.
- setAttributeNS(nsURI, qName, value)
- Parameters:
nsURI – str
qName – str
value – int
This is an overloaded function.
- setAttributeNS(nsURI, qName, value)
- Parameters:
nsURI – str
qName – str
value – int
This is an overloaded function.
- setAttributeNS(nsURI, qName, value)
- Parameters:
nsURI – str
qName – str
value – int
This is an overloaded function.
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.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.- setTagName(name)¶
- Parameters:
name – str
Sets this element’s tag name to
name.See also
- tagName()¶
- Return type:
str
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Returns the tag name of this element. For an XML element like this:
<img src="myimg.png">
the tagname would return “img”.
See also
- text()¶
- Return type:
str
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Returns the element’s text or an empty string.
Example:
<h1>Hello <b>Qt</b> <not [CDATA[<xml is cool>]]></h1>
The function text() 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.