Home · Overviews · Examples 

QDomDocument Class Reference
[com.trolltech.qt.xml module]

The QDomDocument class represents an XML document. More...

Inherits QDomNode.


Detailed Description

The QDomDocument class represents an XML document.

The QDomDocument class represents the entire XML document. Conceptually, it is the root of the document tree, and provides the primary access to the document's data.

Since elements, text nodes, comments, processing instructions, etc., cannot exist outside the context of a document, the document class also contains the factory functions needed to create these objects. The node objects created have an ownerDocument function which associates them with the document within whose context they were created. The DOM classes that will be used most often are QDomNode, QDomDocument, QDomElement and QDomText.

The parsed XML is represented internally by a tree of objects that can be accessed using the various QDom classes. All QDom classes only reference objects in the internal tree. The internal objects in the DOM tree will get deleted once the last QDom object referencing them and the QDomDocument itself are deleted.

Creation of elements, text nodes, etc. is done using the various factory functions provided in this class. Using the default constructors of the QDom classes will only result in empty objects that cannot be manipulated or inserted into the Document.

The QDomDocument class has several functions for creating document data, for example, createElement, createTextNode, createComment, createCDATASection, createProcessingInstruction, createAttribute and createEntityReference. Some of these functions have versions that support namespaces, i.e. createElementNS and createAttributeNS. The createDocumentFragment function is used to hold parts of the document; this is useful for manipulating for complex documents.

The entire content of the document is set with setContent(). This function parses the string it is passed as an XML document and creates the DOM tree that represents the document. The root element is available using documentElement. The textual representation of the document can be obtained using toString.

It is possible to insert a node from another document into the document using importNode.

You can obtain a list of all the elements that have a particular tag with elementsByTagName or with elementsByTagNameNS.

The QDom classes are typically used as follows:

    QDomDocument doc("mydocument");
    QFile file("mydocument.xml");
    if (!file.open(QIODevice::ReadOnly))
        return;
    if (!doc.setContent(&file)) {
        file.close();
        return;
    }
    file.close();

    // print out the element names of all elements that are direct children
    // of the outermost element.
    QDomElement docElem = doc.documentElement();

    QDomNode n = docElem.firstChild();
    while(!n.isNull()) {
        QDomElement e = n.toElement(); // try to convert the node to an element.
        if(!e.isNull()) {
            cout << e.tagName() << endl; // the node really is an element.
        }
        n = n.nextSibling();
    }

    // Here we append a new element to the end of the document
    QDomElement elem = doc.createElement("img");
    elem.setAttribute("src", "myimage.png");
    docElem.appendChild(elem);

Once doc and elem go out of scope, the whole internal tree representing the XML document is deleted.

To create a document using DOM use code like this:

    QDomDocument doc("MyML");
    QDomElement root = doc.createElement("MyML");
    doc.appendChild(root);

    QDomElement tag = doc.createElement("Greeting");
    root.appendChild(tag);

    QDomText t = doc.createTextNode("Hello World");
    tag.appendChild(t);

    QString xml = doc.toString();

For further information about the Document Object Model see the Document Object Model (DOM) Level 1 and Level 2 Core Specifications.

See also DOM Bookmarks Example and Simple DOM Model Example.


Copyright © 2007 Trolltech Trademarks
Qt Jambi 4.3.2_01