Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions

TQDomDocument Class Reference
[XML module]

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

All the functions in this class are reentrant when TQt is built with thread support.

#include <tqdom.h>

Inherits TQDomNode.

List of all member functions.

Public Members


Detailed Description

The TQDomDocument class represents an XML document.

The TQDomDocument 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 TQDomNode, TQDomDocument, TQDomElement and TQDomText.

The parsed XML is represented internally by a tree of objects that can be accessed using the various TQDom classes. All TQDom classes only reference objects in the internal tree. The internal objects in the DOM tree will get deleted once the last TQDom object referencing them and the TQDomDocument 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 TQDom classes will only result in empty objects that cannot be manipulated or inserted into the Document.

The TQDomDocument 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 TQDom classes are typically used as follows:

    TQDomDocument doc( "mydocument" );
    TQFile 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.
    TQDomElement docElem = doc.documentElement();

    TQDomNode n = docElem.firstChild();
    while( !n.isNull() ) {
        TQDomElement 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
    TQDomElement 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:

    TQDomDocument doc( "MyML" );
    TQDomElement root = doc.createElement( "MyML" );
    doc.appendChild( root );

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

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

    TQString 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 TQDomDocument documentation.

See also XML.


Member Function Documentation

TQDomDocument::TQDomDocument ()

Constructs an empty document.

explicit TQDomDocument::TQDomDocument ( const TQString & name )

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

explicit TQDomDocument::TQDomDocument ( const TQDomDocumentType & doctype )

Creates a document with the document type doctype.

See also TQDomImplementation::createDocumentType().

TQDomDocument::TQDomDocument ( const TQDomDocument & 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().

TQDomDocument::~TQDomDocument ()

Destroys the object and frees its resources.

TQDomAttr TQDomDocument::createAttribute ( const TQString & name )

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

See also createAttributeNS().

TQDomAttr TQDomDocument::createAttributeNS ( const TQString & nsURI, const TQString & 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 TQDomNode::prefix() and TQDomNode::localName() to appropriate values (depending on qName).

See also createAttribute().

TQDomCDATASection TQDomDocument::createCDATASection ( const TQString & value )

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

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

TQDomComment TQDomDocument::createComment ( const TQString & value )

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

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

TQDomDocumentFragment TQDomDocument::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.

TQDomElement TQDomDocument::createElement ( const TQString & tagName )

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

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

TQDomElement TQDomDocument::createElementNS ( const TQString & nsURI, const TQString & 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 TQDomNode::prefix() and TQDomNode::localName() to appropriate values (depending on qName).

See also createElement().

TQDomEntityReference TQDomDocument::createEntityReference ( const TQString & name )

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

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

TQDomProcessingInstruction TQDomDocument::createProcessingInstruction ( const TQString & target, const TQString & data )

Creates a new processing instruction that can be inserted into the document, e.g. using TQDomNode::appendChild(). This function sets the target for the processing instruction to target and the data to data.

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

TQDomText TQDomDocument::createTextNode ( const TQString & value )

Creates a text node for the string value that can be inserted into the document tree, e.g. using TQDomNode::appendChild().

Warning: All characters within an XML document must be in the range:

#x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]

This rule also applies to characters encoded as character entities and characters in CDATA sections. If you use this function to insert characters outside of this range, the document will not be well-formed.

If you want to store binary data in an XML document you must either use your own scheme to escape illegal characters, or you must store it in an external unparsed entity.

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

TQDomDocumentType TQDomDocument::doctype () const

Returns the document type of this document.

TQDomElement TQDomDocument::documentElement () const

Returns the root element of the document.

TQDomElement TQDomDocument::elementById ( const TQString & elementId )

Returns the element whose ID is equal to elementId. If no element with the ID was found, this function returns a null element.

Since the TQDomClasses do not know which attributes are element IDs, this function returns always a null element. This may change in a future version.

TQDomNodeList TQDomDocument::elementsByTagName ( const TQString & tagname ) const

Returns a TQDomNodeList, that contains all the elements in the document with the name tagname. The order of the node list is the order they are encountered in a preorder traversal of the element tree.

See also elementsByTagNameNS() and TQDomElement::elementsByTagName().

TQDomNodeList TQDomDocument::elementsByTagNameNS ( const TQString & nsURI, const TQString & localName )

Returns a TQDomNodeList that contains all the elements in the document with the local name localName and a namespace URI of nsURI. The order of the node list is the order they are encountered in a preorder traversal of the element tree.

See also elementsByTagName() and TQDomElement::elementsByTagNameNS().

TQDomImplementation TQDomDocument::implementation () const

Returns a TQDomImplementation object.

TQDomNode TQDomDocument::importNode ( const TQDomNode & importedNode, bool deep )

Imports the node importedNode from another document to this document. importedNode remains in the original document; this function creates a copy that can be used within this document.

This function returns the imported node that belongs to this document. The returned node has no parent. It is not possible to import TQDomDocument and TQDomDocumentType nodes. In those cases this function returns a null node.

If deep is TRUE, this function imports not only the node importedNode but its whole subtree; if it is FALSE, only the importedNode is imported. The argument deep has no effect on TQDomAttr and TQDomEntityReference nodes, since the descendents of TQDomAttr nodes are always imported and those of TQDomEntityReference nodes are never imported.

The behavior of this function is slightly different depending on the node types:

Node Type Behaviour
TQDomAttr The owner element is set to 0 and the specified flag is set to TRUE in the generated attribute. The whole subtree of importedNode is always imported for attribute nodes: deep has no effect.
TQDomDocument Document nodes cannot be imported.
TQDomDocumentFragment If deep is TRUE, this function imports the whole document fragment; otherwise it only generates an empty document fragment.
TQDomDocumentType Document type nodes cannot be imported.
TQDomElement Attributes for which TQDomAttr::specified() is TRUE are also imported, other attributes are not imported. If deep is TRUE, this function also imports the subtree of importedNode; otherwise it imports only the element node (and some attributes, see above).
TQDomEntity Entity nodes can be imported, but at the moment there is no way to use them since the document type is read-only in DOM level 2.
TQDomEntityReference Descendents of entity reference nodes are never imported: deep has no effect.
TQDomNotation Notation nodes can be imported, but at the moment there is no way to use them since the document type is read-only in DOM level 2.
TQDomProcessingInstruction The target and value of the processing instruction is copied to the new node.
TQDomText The text is copied to the new node.
TQDomCDATASection The text is copied to the new node.
TQDomComment The text is copied to the new node.

See also TQDomElement::setAttribute(), TQDomNode::insertBefore(), TQDomNode::insertAfter(), TQDomNode::replaceChild(), TQDomNode::removeChild(), and TQDomNode::appendChild().

bool TQDomDocument::isDocument () const [virtual]

Returns TRUE.

Reimplemented from TQDomNode.

TQDomNode::NodeType TQDomDocument::nodeType () const [virtual]

Returns DocumentNode.

Reimplemented from TQDomNode.

TQDomDocument & TQDomDocument::operator= ( const TQDomDocument & x )

Assigns x to this DOM document.

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

bool TQDomDocument::setContent ( const TQByteArray & buffer, bool namespaceProcessing, TQString * errorMsg = 0, int * errorLine = 0, int * errorColumn = 0 )

This function parses the XML document from the byte array buffer and sets it as the content of the document. It tries to detect the encoding of the document as required by the XML specification.

If namespaceProcessing is TRUE, the parser recognizes namespaces in the XML file and sets the prefix name, local name and namespace URI to appropriate values. If namespaceProcessing is FALSE, the parser does no namespace processing when it reads the XML file.

If a parse error occurs, the function returns FALSE; otherwise it returns TRUE. If a parse error occurs and errorMsg, errorLine and errorColumn are not 0, the error message is placed in *errorMsg, the line number *errorLine and the column number in *errorColumn.

If namespaceProcessing is TRUE, the function TQDomNode::prefix() returns a string for all elements and attributes. It returns an empty string if the element or attribute has no prefix.

If namespaceProcessing is FALSE, the functions TQDomNode::prefix(), TQDomNode::localName() and TQDomNode::namespaceURI() return TQString::null.

See also TQDomNode::namespaceURI(), TQDomNode::localName(), TQDomNode::prefix(), TQString::isNull(), and TQString::isEmpty().

bool TQDomDocument::setContent ( const TQCString & buffer, bool namespaceProcessing, TQString * errorMsg = 0, int * errorLine = 0, int * errorColumn = 0 )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

This function reads the XML document from the C string buffer.

Warning: This function does not try to detect the encoding: instead it assumes that the C string is UTF-8 encoded.

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

This function reads the XML document from the string text. Since text is already a Unicode string, no encoding detection is done.

bool TQDomDocument::setContent ( TQIODevice * dev, bool namespaceProcessing, TQString * errorMsg = 0, int * errorLine = 0, int * errorColumn = 0 )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

This function reads the XML document from the IO device dev.

bool TQDomDocument::setContent ( const TQCString & buffer, TQString * errorMsg = 0, int * errorLine = 0, int * errorColumn = 0 )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

This function reads the XML document from the C string buffer.

No namespace processing is performed.

Warning: This function does not try to detect the encoding: instead it assumes that the C string is UTF-8 encoded.

bool TQDomDocument::setContent ( const TQByteArray & buffer, TQString * errorMsg = 0, int * errorLine = 0, int * errorColumn = 0 )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

This function reads the XML document from the byte array buffer.

No namespace processing is performed.

bool TQDomDocument::setContent ( const TQString & text, TQString * errorMsg = 0, int * errorLine = 0, int * errorColumn = 0 )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

This function reads the XML document from the string text. Since text is already a Unicode string, no encoding detection is performed.

No namespace processing is performed either.

bool TQDomDocument::setContent ( TQIODevice * dev, TQString * errorMsg = 0, int * errorLine = 0, int * errorColumn = 0 )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

This function reads the XML document from the IO device dev.

No namespace processing is performed.

bool TQDomDocument::setContent ( TQXmlInputSource * source, TQXmlReader * reader, TQString * errorMsg = 0, int * errorLine = 0, int * errorColumn = 0 )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

This function reads the XML document from the TQXmlInputSource source and parses it with the TQXmlReader reader.

This function doesn't change the features of the reader. If you want to use certain features for parsing you can use this function to set up the reader appropriate.

See also TQXmlSimpleReader.

TQCString TQDomDocument::toCString () const

Converts the parsed document back to its textual representation and returns a TQCString for that is encoded in UTF-8.

See also toString().

TQCString TQDomDocument::toCString ( int indent ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

This function uses indent as the amount of space to indent subelements.

TQString TQDomDocument::toString () const

Converts the parsed document back to its textual representation.

See also toCString().

TQString TQDomDocument::toString ( int indent ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

This function uses indent as the amount of space to indent subelements.


This file is part of the TQt toolkit. Copyright © 1995-2007 Trolltech. All Rights Reserved.


Copyright © 2007 TrolltechTrademarks
TQt 3.3.8