QDomElement

The QDomElement class represents one element in the DOM tree. More

Inheritance diagram of PySide6.QtXml.QDomElement

Synopsis

Functions

Detailed Description

Elements have a tagName() and zero or more attributes associated with them. The tag name can be changed with setTagName() .

Element attributes are represented by QDomAttr objects that can be queried using the 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() .

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 for QDomText nodes, 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 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 = // ...
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 QDomDocument documentation.

class PySide6.QtXml.QDomElement

PySide6.QtXml.QDomElement(x)

Parameters

xPySide6.QtXml.QDomElement

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

Constructs a copy of x.

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() .

PySide6.QtXml.QDomElement.attribute(name[, defValue=""])
Parameters
  • name – str

  • defValue – str

Return type

str

Returns the attribute called name. If the attribute does not exist defValue is returned.

PySide6.QtXml.QDomElement.attributeNS(nsURI, localName[, defValue=""])
Parameters
  • nsURI – str

  • localName – str

  • defValue – str

Return type

str

Returns the attribute with the local name localName and the namespace URI nsURI. If the attribute does not exist defValue is returned.

PySide6.QtXml.QDomElement.attributeNode(name)
Parameters

name – str

Return type

PySide6.QtXml.QDomAttr

Returns the QDomAttr object that corresponds to the attribute called name. If no such attribute exists a null attribute is returned.

PySide6.QtXml.QDomElement.attributeNodeNS(nsURI, localName)
Parameters
  • nsURI – str

  • localName – str

Return type

PySide6.QtXml.QDomAttr

Returns the QDomAttr object that corresponds to the attribute with the local name localName and the namespace URI nsURI. If no such attribute exists a null attribute is returned.

PySide6.QtXml.QDomElement.elementsByTagName(tagname)
Parameters

tagname – str

Return type

PySide6.QtXml.QDomNodeList

Returns a QDomNodeList containing all descendants of this element named tagname encountered 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.

PySide6.QtXml.QDomElement.elementsByTagNameNS(nsURI, localName)
Parameters
  • nsURI – str

  • localName – str

Return type

PySide6.QtXml.QDomNodeList

Returns a QDomNodeList containing all descendants of this element with local name localName and namespace URI nsURI encountered 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.

PySide6.QtXml.QDomElement.hasAttribute(name)
Parameters

name – str

Return type

bool

Returns true if this element has an attribute called name; otherwise returns false.

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.

PySide6.QtXml.QDomElement.hasAttributeNS(nsURI, localName)
Parameters
  • nsURI – str

  • localName – str

Return type

bool

Returns true if this element has an attribute with the local name localName and the namespace URI nsURI; otherwise returns false.

PySide6.QtXml.QDomElement.removeAttribute(name)
Parameters

name – str

Removes the attribute called name name from this element.

PySide6.QtXml.QDomElement.removeAttributeNS(nsURI, localName)
Parameters
  • nsURI – str

  • localName – str

Removes the attribute with the local name localName and the namespace URI nsURI from this element.

PySide6.QtXml.QDomElement.removeAttributeNode(oldAttr)
Parameters

oldAttrPySide6.QtXml.QDomAttr

Return type

PySide6.QtXml.QDomAttr

Removes the attribute oldAttr from the element and returns it.

PySide6.QtXml.QDomElement.setAttribute(name, value)
Parameters
  • name – str

  • value – str

PySide6.QtXml.QDomElement.setAttribute(name, value)
Parameters
  • name – str

  • valuedouble

PySide6.QtXml.QDomElement.setAttribute(name, value)
Parameters
  • name – str

  • valueqlonglong

PySide6.QtXml.QDomElement.setAttributeNS(nsURI, qName, value)
Parameters
  • nsURI – str

  • qName – str

  • valueuint

PySide6.QtXml.QDomElement.setAttributeNS(nsURI, qName, value)
Parameters
  • nsURI – str

  • qName – str

  • valuequlonglong

PySide6.QtXml.QDomElement.setAttributeNS(nsURI, qName, value)
Parameters
  • nsURI – str

  • qName – str

  • valueqlonglong

PySide6.QtXml.QDomElement.setAttributeNS(nsURI, qName, value)
Parameters
  • nsURI – str

  • qName – str

  • valuedouble

PySide6.QtXml.QDomElement.setAttributeNS(nsURI, qName, value)
Parameters
  • nsURI – str

  • qName – str

  • value – str

PySide6.QtXml.QDomElement.setAttributeNS(nsURI, qName, value)
Parameters
  • nsURI – str

  • qName – str

  • value – int

PySide6.QtXml.QDomElement.setAttributeNode(newAttr)
Parameters

newAttrPySide6.QtXml.QDomAttr

Return type

PySide6.QtXml.QDomAttr

Adds the attribute newAttr to 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 a null attribute .

PySide6.QtXml.QDomElement.setAttributeNodeNS(newAttr)
Parameters

newAttrPySide6.QtXml.QDomAttr

Return type

PySide6.QtXml.QDomAttr

Adds the attribute newAttr to 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 a null attribute .

PySide6.QtXml.QDomElement.setTagName(name)
Parameters

name – str

Sets this element’s tag name to name.

See also

tagName()

PySide6.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

setTagName()

PySide6.QtXml.QDomElement.text()
Return type

str

Returns the element’s text or an empty string.

Example:

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

The function of the QDomElement for the <h1> tag, will return the following text:

Qt = Hello()

Comments are ignored by this function. It only evaluates QDomText and QDomCDATASection objects.