Table Of Contents

Previous topic

QDomNotation

Next topic

QDomAttr

QDomElement

Synopsis

Functions

Detailed Description

The PySide.QtXml.QDomElement class represents one element in the DOM tree.

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

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

If you want to access the text of a node use PySide.QtXml.QDomElement.text() , e.g.

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

The 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 documentation.

class PySide.QtXml.QDomElement
class PySide.QtXml.QDomElement(x)
Parameters:xPySide.QtXml.QDomElement

Constructs an empty element. Use the QDomDocument.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 PySide.QtXml.QDomNode.cloneNode() .

PySide.QtXml.QDomElement.attribute(name[, defValue=""])
Parameters:
  • name – unicode
  • defValue – unicode
Return type:

unicode

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

PySide.QtXml.QDomElement.attributeNS(nsURI, localName[, defValue=""])
Parameters:
  • nsURI – unicode
  • localName – unicode
  • defValue – unicode
Return type:

unicode

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

PySide.QtXml.QDomElement.attributeNode(name)
Parameters:name – unicode
Return type:PySide.QtXml.QDomAttr

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

PySide.QtXml.QDomElement.attributeNodeNS(nsURI, localName)
Parameters:
  • nsURI – unicode
  • localName – unicode
Return type:

PySide.QtXml.QDomAttr

Returns the PySide.QtXml.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.

PySide.QtXml.QDomElement.elementsByTagName(tagname)
Parameters:tagname – unicode
Return type:PySide.QtXml.QDomNodeList

Returns a PySide.QtXml.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.

PySide.QtXml.QDomElement.elementsByTagNameNS(nsURI, localName)
Parameters:
  • nsURI – unicode
  • localName – unicode
Return type:

PySide.QtXml.QDomNodeList

Returns a PySide.QtXml.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.

PySide.QtXml.QDomElement.hasAttribute(name)
Parameters:name – unicode
Return type:PySide.QtCore.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 PySide.QtXml.QDomElement.hasAttributeNS() to explicitly test for attributes with specific namespaces and names.

PySide.QtXml.QDomElement.hasAttributeNS(nsURI, localName)
Parameters:
  • nsURI – unicode
  • localName – unicode
Return type:

PySide.QtCore.bool

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

PySide.QtXml.QDomElement.removeAttribute(name)
Parameters:name – unicode

Removes the attribute called name name from this element.

PySide.QtXml.QDomElement.removeAttributeNS(nsURI, localName)
Parameters:
  • nsURI – unicode
  • localName – unicode

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

PySide.QtXml.QDomElement.removeAttributeNode(oldAttr)
Parameters:oldAttrPySide.QtXml.QDomAttr
Return type:PySide.QtXml.QDomAttr

Removes the attribute oldAttr from the element and returns it.

PySide.QtXml.QDomElement.setAttribute(name, value)
Parameters:
  • name – unicode
  • valuePySide.QtCore.qulonglong

This is an overloaded function.

The number is formatted according to the current locale.

PySide.QtXml.QDomElement.setAttribute(name, value)
Parameters:
  • name – unicode
  • valuePySide.QtCore.uint

This is an overloaded function.

The number is formatted according to the current locale.

PySide.QtXml.QDomElement.setAttribute(name, value)
Parameters:
  • name – unicode
  • valuePySide.QtCore.int

This is an overloaded function.

The number is formatted according to the current locale.

PySide.QtXml.QDomElement.setAttribute(name, value)
Parameters:
  • name – unicode
  • valuePySide.QtCore.qlonglong

This is an overloaded function.

The number is formatted according to the current locale.

PySide.QtXml.QDomElement.setAttribute(name, value)
Parameters:
  • name – unicode
  • valuePySide.QtCore.double

This is an overloaded function.

The number is formatted according to the current locale.

PySide.QtXml.QDomElement.setAttribute(name, value)
Parameters:
  • name – unicode
  • value – unicode

Adds an attribute called name with value value . If an attribute with the same name exists, its value is replaced by value .

PySide.QtXml.QDomElement.setAttribute(name, value)
Parameters:
  • name – unicode
  • valuePySide.QtCore.float

This is an overloaded function.

The number is formatted according to the current locale.

PySide.QtXml.QDomElement.setAttributeNS(nsURI, qName, value)
Parameters:
  • nsURI – unicode
  • qName – unicode
  • valuePySide.QtCore.qulonglong

This is an overloaded function.

PySide.QtXml.QDomElement.setAttributeNS(nsURI, qName, value)
Parameters:
  • nsURI – unicode
  • qName – unicode
  • valuePySide.QtCore.uint

This is an overloaded function.

PySide.QtXml.QDomElement.setAttributeNS(nsURI, qName, value)
Parameters:
  • nsURI – unicode
  • qName – unicode
  • valuePySide.QtCore.qlonglong

This is an overloaded function.

PySide.QtXml.QDomElement.setAttributeNS(nsURI, qName, value)
Parameters:
  • nsURI – unicode
  • qName – unicode
  • valuePySide.QtCore.int

This is an overloaded function.

PySide.QtXml.QDomElement.setAttributeNS(nsURI, qName, value)
Parameters:
  • nsURI – unicode
  • qName – unicode
  • valuePySide.QtCore.double

This is an overloaded function.

PySide.QtXml.QDomElement.setAttributeNS(nsURI, qName, value)
Parameters:
  • nsURI – unicode
  • qName – unicode
  • value – unicode

Adds an attribute with the qualified name qName and the namespace URI nsURI with the value 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 .

Although qName is the qualified name, the local name is used to decide if an existing attribute’s value should be replaced.

PySide.QtXml.QDomElement.setAttributeNode(newAttr)
Parameters:newAttrPySide.QtXml.QDomAttr
Return type:PySide.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 .

PySide.QtXml.QDomElement.setAttributeNodeNS(newAttr)
Parameters:newAttrPySide.QtXml.QDomAttr
Return type:PySide.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 .

PySide.QtXml.QDomElement.setTagName(name)
Parameters:name – unicode

Sets this element’s tag name to name .

PySide.QtXml.QDomElement.tagName()
Return type:unicode

Returns the tag name of this element. For an XML element like this:

<img src="myimg.png">

the tagname would return “img”.

PySide.QtXml.QDomElement.text()
Return type:unicode

Returns the element’s text or an empty string.

Example:

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

The function PySide.QtXml.QDomElement.text() of the PySide.QtXml.QDomElement for the <h1> tag, will return the following text:

Hello Qt <xml is cool>

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