![]() |
Home · Overviews · Examples | ![]() |
The QDomElement class represents one element in the DOM tree. More...
Inherits QDomNode.
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.nextSiblingElelement("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.
Copyright © 2008 Trolltech | Trademarks | Qt Jambi 4.3.4_01 |