|  | Home · Overviews · Examples

# QDomNode Class Reference
[com.trolltech.qt.xml module]

The `QDomNode` class is the base class for all the nodes in a DOM tree. More...

Inherited by `QDomAttr`, `QDomCharacterData`, `QDomDocument`, `QDomDocumentFragment`, `QDomDocumentType`, `QDomElement`, `QDomEntity`, `QDomEntityReference`, `QDomNotation`, and `QDomProcessingInstruction`.

## Detailed Description

The `QDomNode` class is the base class for all the nodes in a DOM tree.

Many functions in the DOM return a `QDomNode`.

You can find out the type of a node using `isAttr`, `isCDATASection`, `isDocumentFragment`, `isDocument`, `isDocumentType`, `isElement`, `isEntityReference`, `isText`, `isEntity`, `isNotation`, `isProcessingInstruction`, `isCharacterData` and `isComment`.

A `QDomNode` can be converted into one of its subclasses using `toAttr`, `toCDATASection`, `toDocumentFragment`, `toDocument`, `toDocumentType`, `toElement`, `toEntityReference`, `toText`, `toEntity`, `toNotation`, `toProcessingInstruction`, `toCharacterData` or `toComment`. You can convert a node to a null node with `clear`.

Copies of the `QDomNode` class share their data using explicit sharing. This means that modifying one node will change all copies. This is especially useful in combination with functions which return a `QDomNode`, e.g. `firstChild`. You can make an independent (deep) copy of the node with `cloneNode`.

A `QDomNode` can be null, much like a null pointer. Creating a copy of a null node results in another null node. It is not possible to modify a null node, but it is possible to assign another, possibly non-null node to it. In this case, the copy of the null node will remain null. You can check if a `QDomNode` is null by calling `isNull`. The empty constructor of a `QDomNode` (or any of the derived classes) creates a null node.

Nodes are inserted with `insertBefore`, `insertAfter` or `appendChild`. You can replace one node with another using `replaceChild` and remove a node with `removeChild`.

To traverse nodes use `firstChild` to get a node's first child (if any), and `nextSibling` to traverse. `QDomNode` also provides `lastChild`, `previousSibling` and `parentNode`. To find the first child node with a particular node name use `namedItem`.

To find out if a node has children use `hasChildNodes` and to get a list of all of a node's children use `childNodes`.

The node's name and value (the meaning of which varies depending on its type) is returned by `nodeName` and `nodeValue` respectively. The node's type is returned by `nodeType`. The node's value can be set with `setNodeValue`.

The document to which the node belongs is returned by `ownerDocument`.

Adjacent `QDomText` nodes can be merged into a single node with `normalize`.

`QDomElement` nodes have attributes which can be retrieved with attributes().

`QDomElement` and `QDomAttr` nodes can have namespaces which can be retrieved with `namespaceURI`. Their local name is retrieved with `localName`, and their prefix with `prefix`. The prefix can be set with `setPrefix`.

You can write the XML representation of the node to a text stream with `save`.

The following example looks for the first element in an XML document and prints the names of all the elements that are its direct children.

        QDomDocument d;
        d.setContent(someXML);
        QDomNode n = d.firstChild();
        while (!n.isNull()) {
            if (n.isElement()) {
                QDomElement e = n.toElement();
                cout << "Element name: " << e.tagName() << endl;
                break;
            }
            n = n.nextSibling();
        }

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.