QDomDocument

Section: Misc. Reference Manual Pages (3qt)
Updated: 2 February 2007
Index Return to Main Contents
 

NAME

QDomDocument - Represents an XML document  

SYNOPSIS

All the functions in this class are reentrant when Qt is built with thread support.</p>

#include <qdom.h>

Inherits QDomNode.

 

Public Members


QDomDocument ()

explicit QDomDocument ( const QString & name )

explicit QDomDocument ( const QDomDocumentType & doctype )

QDomDocument ( const QDomDocument & x )

QDomDocument & operator= ( const QDomDocument & x )

~QDomDocument ()

QDomElement createElement ( const QString & tagName )

QDomDocumentFragment createDocumentFragment ()

QDomText createTextNode ( const QString & value )

QDomComment createComment ( const QString & value )

QDomCDATASection createCDATASection ( const QString & value )

QDomProcessingInstruction createProcessingInstruction ( const QString & target, const QString & data )

QDomAttr createAttribute ( const QString & name )

QDomEntityReference createEntityReference ( const QString & name )

QDomNodeList elementsByTagName ( const QString & tagname ) const

QDomNode importNode ( const QDomNode & importedNode, bool deep )

QDomElement createElementNS ( const QString & nsURI, const QString & qName )

QDomAttr createAttributeNS ( const QString & nsURI, const QString & qName )

QDomNodeList elementsByTagNameNS ( const QString & nsURI, const QString & localName )

QDomElement elementById ( const QString & elementId )

QDomDocumentType doctype () const

QDomImplementation implementation () const

QDomElement documentElement () const

bool setContent ( const QCString & buffer, bool namespaceProcessing, QString * errorMsg = 0, int * errorLine = 0, int * errorColumn = 0 )

bool setContent ( const QByteArray & buffer, bool namespaceProcessing, QString * errorMsg = 0, int * errorLine = 0, int * errorColumn = 0 )

bool setContent ( const QString & text, bool namespaceProcessing, QString * errorMsg = 0, int * errorLine = 0, int * errorColumn = 0 )

bool setContent ( QIODevice * dev, bool namespaceProcessing, QString * errorMsg = 0, int * errorLine = 0, int * errorColumn = 0 )

bool setContent ( const QCString & buffer, QString * errorMsg = 0, int * errorLine = 0, int * errorColumn = 0 )

bool setContent ( const QByteArray & buffer, QString * errorMsg = 0, int * errorLine = 0, int * errorColumn = 0 )

bool setContent ( const QString & text, QString * errorMsg = 0, int * errorLine = 0, int * errorColumn = 0 )

bool setContent ( QIODevice * dev, QString * errorMsg = 0, int * errorLine = 0, int * errorColumn = 0 )

bool setContent ( QXmlInputSource * source, QXmlReader * reader, QString * errorMsg = 0, int * errorLine = 0, int * errorColumn = 0 )

virtual QDomNode::NodeType nodeType () const

virtual bool isDocument () const

QString toString () const

QString toString ( int indent ) const

QCString toCString () const

QCString toCString ( int indent ) const
 

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( IO_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 http://www.w3.org/TR/REC-DOM-Level-1/ and http://www.w3.org/TR/DOM-Level-2-Core/. For a more general introduction of the DOM implementation see the QDomDocument documentation.

See also XML.  

MEMBER FUNCTION DOCUMENTATION

 

QDomDocument::QDomDocument ()

Constructs an empty document.  

explicit QDomDocument::QDomDocument ( const QString & name )

Creates a document and sets the name of the document type to name.  

explicit QDomDocument::QDomDocument ( const QDomDocumentType & doctype )

Creates a document with the document type doctype.

See also QDomImplementation::createDocumentType().  

QDomDocument::QDomDocument ( const QDomDocument & x )

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

QDomDocument::~QDomDocument ()

Destroys the object and frees its resources.  

QDomAttr QDomDocument::createAttribute ( const QString & name )

Creates a new attribute called name that can be inserted into an element, e.g. using QDomElement::setAttributeNode().

See also createAttributeNS().  

QDomAttr QDomDocument::createAttributeNS ( const QString & nsURI, const QString & qName )

Creates a new attribute with namespace support that can be inserted into an element. The name of the attribute is qName and the namespace URI is nsURI. This function also sets QDomNode::prefix() and QDomNode::localName() to appropriate values (depending on qName).

See also createAttribute().  

QDomCDATASection QDomDocument::createCDATASection ( const QString & value )

Creates a new CDATA section for the string value that can be inserted into the document, e.g. using QDomNode::appendChild().

See also QDomNode::appendChild(), QDomNode::insertBefore(), and QDomNode::insertAfter().  

QDomComment QDomDocument::createComment ( const QString & value )

Creates a new comment for the string value that can be inserted into the document, e.g. using QDomNode::appendChild().

See also QDomNode::appendChild(), QDomNode::insertBefore(), and QDomNode::insertAfter().  

QDomDocumentFragment QDomDocument::createDocumentFragment ()

Creates a new document fragment, that can be used to hold parts of the document, e.g. when doing complex manipulations of the document tree.  

QDomElement QDomDocument::createElement ( const QString & tagName )

Creates a new element called tagName that can be inserted into the DOM tree, e.g. using QDomNode::appendChild().

See also createElementNS(), QDomNode::appendChild(), QDomNode::insertBefore(), and QDomNode::insertAfter().  

QDomElement QDomDocument::createElementNS ( const QString & nsURI, const QString & qName )

Creates a new element with namespace support that can be inserted into the DOM tree. The name of the element is qName and the namespace URI is nsURI. This function also sets QDomNode::prefix() and QDomNode::localName() to appropriate values (depending on qName).

See also createElement().  

QDomEntityReference QDomDocument::createEntityReference ( const QString & name )

Creates a new entity reference called name that can be inserted into the document, e.g. using QDomNode::appendChild().

See also QDomNode::appendChild(), QDomNode::insertBefore(), and QDomNode::insertAfter().  

QDomProcessingInstruction QDomDocument::cre