|  | Home · All Classes · Modules

# QDomElement Class Reference
[QtXml module]

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

Inherits QDomNode.

### Methods

- __init__ (self)
- __init__ (self, QDomElement)
- QStringQString attribute (self, QStringQString, QStringQString defaultValue = QString())
- QDomAttr attributeNode (self, QStringQString)
- QDomAttr attributeNodeNS (self, QStringQString, QStringQString)
- QStringQString attributeNS (self, QStringQString, QStringQString, QStringQString defaultValue = QString())
- QDomNamedNodeMap attributes (self)
- QDomNodeList elementsByTagName (self, QStringQString)
- QDomNodeList elementsByTagNameNS (self, QStringQString, QStringQString)
- bool hasAttribute (self, QStringQString)
- bool hasAttributeNS (self, QStringQString, QStringQString)
- QDomNode.NodeType nodeType (self)
- removeAttribute (self, QStringQString)
- QDomAttr removeAttributeNode (self, QDomAttr)
- removeAttributeNS (self, QStringQString, QStringQString)
- setAttribute (self, QStringQString, QStringQString)
- setAttribute (self, QStringQString, int)
- setAttribute (self, QStringQString, int)
- setAttribute (self, QStringQString, float)
- setAttribute (self, QStringQString, int)
- QDomAttr setAttributeNode (self, QDomAttr)
- QDomAttr setAttributeNodeNS (self, QDomAttr)
- setAttributeNS (self, QStringQString, QStringQString, QStringQString)
- setAttributeNS (self, QStringQString, QStringQString, int)
- setAttributeNS (self, QStringQString, QStringQString, int)
- setAttributeNS (self, QStringQString, QStringQString, float)
- setAttributeNS (self, QStringQString, QStringQString, int)
- setTagName (self, QStringQString)
- QStringQString tagName (self)
- QStringQString text (self)

## Detailed Description

The QDomElement class represents one element in the DOM tree.

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.

     QDomElement e = //...
     //...
     QString 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.

     QString text;
     QDomElement element = doc.documentElement();
     for(QDomNode n = element.firstChild(); !n.isNull(); n = n.nextSibling())
     {
         QDomText t = n.toText();
         if (!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:

     QDomDocument doc = // ...
     QDomElement root = doc.firstChildElement("database");
     QDomElement elt = root.firstChildElement("entry");
     for (; !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.

## Method Documentation

### QDomElement.__init__ (self)

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

### QDomElement.__init__ (self, QDomElement)

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

### QStringQString QDomElement.attribute (self, QStringQString, QStringQString defaultValue = QString())

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

See also setAttribute(), attributeNode(), setAttributeNode(), and attributeNS().

### QDomAttr QDomElement.attributeNode (self, QStringQString)

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

See also setAttributeNode(), attribute(), setAttribute(), and attributeNodeNS().

### QDomAttr QDomElement.attributeNodeNS (self, QStringQString, QStringQString)

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.

See also setAttributeNodeNS(), setAttributeNode(), attribute(), and setAttribute().

### QStringQString QDomElement.attributeNS (self, QStringQString, QStringQString, QStringQString defaultValue = QString())

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

See also setAttributeNS(), attributeNodeNS(), setAttributeNodeNS(), and attribute().

### QDomNamedNodeMap QDomElement.attributes (self)

Returns a QDomNamedNodeMap containing all this element's attributes.

See also attribute(), setAttribute(), attributeNode(), and setAttributeNode().

### QDomNodeList QDomElement.elementsByTagName (self, QStringQString)

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.

See also elementsByTagNameNS() and QDomDocument.elementsByTagName().

### QDomNodeList QDomElement.elementsByTagNameNS (self, QStringQString, QStringQString)

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.

See also elementsByTagName() and QDomDocument.elementsByTagNameNS().

### bool QDomElement.hasAttribute (self, QStringQString)

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.

### bool QDomElement.hasAttributeNS (self, QStringQString, QStringQString)

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

### QDomNode.NodeType QDomElement.nodeType (self)

Returns `ElementNode`.

### QDomElement.removeAttribute (self, QStringQString)

Removes the attribute called name name from this element.

See also setAttribute(), attribute(), and removeAttributeNS().

### QDomAttr QDomElement.removeAttributeNode (self, QDomAttr)

Removes the attribute oldAttr from the element and returns it.

See also attributeNode() and setAttributeNode().

### QDomElement.removeAttributeNS (self, QStringQString, QStringQString)

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

See also setAttributeNS(), attributeNS(), and removeAttribute().

### QDomElement.setAttribute (self, QStringQString, QStringQString)

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

See also attribute(), setAttributeNode(), and setAttributeNS().

### QDomElement.setAttribute (self, QStringQString, int)

This is an overloaded function.

The number is formatted according to the current locale.

### QDomElement.setAttribute (self, QStringQString, int)

This is an overloaded function.

The number is formatted according to the current locale.

### QDomElement.setAttribute (self, QStringQString, float)

This is an overloaded function.

The number is formatted according to the current locale.

### QDomElement.setAttribute (self, QStringQString, int)

This is an overloaded function.

The number is formatted according to the current locale.

### QDomAttr QDomElement.setAttributeNode (self, 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.

See also attributeNode(), setAttribute(), and setAttributeNodeNS().

### QDomAttr QDomElement.setAttributeNodeNS (self, 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.

See also attributeNodeNS(), setAttributeNS(), and setAttributeNode().

### QDomElement.setAttributeNS (self, QStringQString, QStringQString, QStringQString)

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.

See also attributeNS(), setAttributeNodeNS(), and setAttribute().

### QDomElement.setAttributeNS (self, QStringQString, QStringQString, int)

This is an overloaded function.

### QDomElement.setAttributeNS (self, QStringQString, QStringQString, int)

This is an overloaded function.

### QDomElement.setAttributeNS (self, QStringQString, QStringQString, float)

This is an overloaded function.

### QDomElement.setAttributeNS (self, QStringQString, QStringQString, int)

This is an overloaded function.

### QDomElement.setTagName (self, QStringQString)

Sets this element's tag name to name.

See also tagName().

### QStringQString QDomElement.tagName (self)

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

### QStringQString QDomElement.text (self)

Returns the element's text or an empty string.

Example:

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

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

     Hello Qt <xml is cool>

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