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

TQDomNode Class Reference
[XML module]

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

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

#include <qdom.h>

Inherited by TQDomDocumentType, TQDomDocument, TQDomDocumentFragment, TQDomCharacterData, TQDomAttr, TQDomElement, TQDomNotation, TQDomEntity, TQDomEntityReference, and TQDomProcessingInstruction.

List of all member functions.

Public Members

Related Functions


Detailed Description

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

Many functions in the DOM return a TQDomNode.

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 TQDomNode 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 TQDomNode 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 TQDomNode, e.g. firstChild(). You can make an independent (deep) copy of the node with cloneNode().

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. TQDomNode 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 TQDomText nodes can be merged into a single node with normalize().

TQDomElement nodes have attributes which can be retrieved with attributes().

TQDomElement and TQDomAttr 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.

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

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 Type Documentation

TQDomNode::NodeType

This enum defines the type of the node:


Member Function Documentation

TQDomNode::TQDomNode ()

Constructs a null node.

TQDomNode::TQDomNode ( const TQDomNode & n )

Constructs a copy of n.

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

TQDomNode::~TQDomNode () [virtual]

Destroys the object and frees its resources.

TQDomNode TQDomNode::appendChild ( const TQDomNode & newChild ) [virtual]

Appends newChild as the node's last child.

If newChild is the child of another node, it is reparented to this node. If newChild is a child of this node, then its position in the list of children is changed.

If newChild is a TQDomDocumentFragment, then the children of the fragment are removed from the fragment and appended.

Returns a new reference to newChild.

See also insertBefore(), insertAfter(), replaceChild(), and removeChild().

TQDomNamedNodeMap TQDomNode::attributes () const [virtual]

Returns a named node map of all attributes. Attributes are only provided for TQDomElements.

Changing the attributes in the map will also change the attributes of this TQDomNode.

Reimplemented in TQDomElement.

TQDomNodeList TQDomNode::childNodes () const [virtual]

Returns a list of all direct child nodes.

Most often you will call this function on a TQDomElement object.

For example, if the XML document looks like this:

    <body>
    <h1>Heading</h1>
    <p>Hello <b>you</b></p>
    </body>
    
Then the list of child nodes for the "body"-element will contain the node created by the <h1> tag and the node created by the <p> tag.

The nodes in the list are not copied; so changing the nodes in the list will also change the children of this node.

See also firstChild() and lastChild().

void TQDomNode::clear ()

Converts the node into a null node; if it was not a null node before, its type and contents are deleted.

See also isNull().

TQDomNode TQDomNode::cloneNode ( bool deep = TRUE ) const [virtual]

Creates a deep (not shallow) copy of the TQDomNode.

If deep is TRUE, then the cloning is done recursively which means that all the node's children are deep copied too. If deep is FALSE only the node itself is copied and the copy will have no child nodes.

TQDomNode TQDomNode::firstChild () const [virtual]

Returns the first child of the node. If there is no child node, a null node is returned. Changing the returned node will also change the node in the document tree.

See also lastChild() and childNodes().

Example: xml/outliner/outlinetree.cpp.

bool TQDomNode::hasAttributes () const [virtual]

Returns TRUE if the node has attributes; otherwise returns FALSE.

See also attributes().

bool TQDomNode::hasChildNodes () const [virtual]

Returns TRUE if the node has one or more children; otherwise returns FALSE.

TQDomNode TQDomNode::insertAfter ( const TQDomNode & newChild, const TQDomNode & refChild ) [virtual]

Inserts the node newChild after the child node refChild. refChild must be a direct child of this node. If refChild is null then newChild is appended as this node's last child.

If newChild is the child of another node, it is reparented to this node. If newChild is a child of this node, then its position in the list of children is changed.

If newChild is a TQDomDocumentFragment, then the children of the fragment are removed from the fragment and inserted after refChild.

Returns a new reference to newChild on success or a null node on failure.

See also insertBefore(), replaceChild(), removeChild(), and appendChild().

TQDomNode TQDomNode::insertBefore ( const TQDomNode & newChild, const TQDomNode & refChild ) [virtual]

Inserts the node newChild before the child node refChild. refChild must be a direct child of this node. If refChild is null then newChild is inserted as the node's first child.

If newChild is the child of another node, it is reparented to this node. If newChild is a child of this node, then its position in the list of children is changed.

If newChild is a TQDomDocumentFragment, then the children of the fragment are removed from the fragment and inserted before refChild.

Returns a new reference to newChild on success or a null node on failure.

See also insertAfter(), replaceChild(), removeChild(), and appendChild().

bool TQDomNode::isAttr () const [virtual]

Returns TRUE if the node is an attribute; otherwise returns FALSE.

If this function returns TRUE, it does not imply that this object is a TQDomAttribute; you can get the TQDomAttribute with toAttribute().

See also toAttr().

Reimplemented in TQDomAttr.

bool TQDomNode::isCDATASection () const [virtual]

Returns TRUE if the node is a CDATA section; otherwise returns FALSE.

If this function returns TRUE, it does not imply that this object is a TQDomCDATASection; you can get the TQDomCDATASection with toCDATASection().

See also toCDATASection().

Reimplemented in TQDomCDATASection.

bool TQDomNode::isCharacterData () const [virtual]

Returns TRUE if the node is a character data node; otherwise returns FALSE.

If this function returns TRUE, it does not imply that this object is a TQDomCharacterData; you can get the TQDomCharacterData with toCharacterData().

See also toCharacterData().

Reimplemented in TQDomCharacterData.

bool TQDomNode::isComment () const [virtual]

Returns TRUE if the node is a comment; otherwise returns FALSE.

If this function returns TRUE, it does not imply that this object is a TQDomComment; you can get the TQDomComment with toComment().

See also toComment().

Reimplemented in TQDomComment.

bool TQDomNode::isDocument () const [virtual]

Returns TRUE if the node is a document; otherwise returns FALSE.

If this function returns TRUE, it does not imply that this object is a TQDomDocument; you can get the TQDomDocument with toDocument().

See also toDocument().

Reimplemented in TQDomDocument.

bool TQDomNode::isDocumentFragment () const [virtual]

Returns TRUE if the node is a document fragment; otherwise returns FALSE.

If this function returns TRUE, it does not imply that this object is a TQDomDocumentFragment; you can get the TQDomDocumentFragment with toDocumentFragment().

See also toDocumentFragment().

Reimplemented in TQDomDocumentFragment.

bool TQDomNode::isDocumentType () const [virtual]

Returns TRUE if the node is a document type; otherwise returns FALSE.

If this function returns TRUE, it does not imply that this object is a TQDomDocumentType; you can get the TQDomDocumentType with toDocumentType().

See also toDocumentType().

Reimplemented in TQDomDocumentType.

bool TQDomNode::isElement () const [virtual]

Returns TRUE if the node is an element; otherwise returns FALSE.

If this function returns TRUE, it does not imply that this object is a TQDomElement; you can get the TQDomElement with toElement().

See also toElement().

Example: xml/outliner/outlinetree.cpp.

Reimplemented in TQDomElement.

bool TQDomNode::isEntity () const [virtual]

Returns TRUE if the node is an entity; otherwise returns FALSE.

If this function returns TRUE, it does not imply that this object is a TQDomEntity; you can get the TQDomEntity with toEntity().

See also toEntity().

Reimplemented in TQDomEntity.

bool TQDomNode::isEntityReference () const [virtual]

Returns TRUE if the node is an entity reference; otherwise returns FALSE.

If this function returns TRUE, it does not imply that this object is a TQDomEntityReference; you can get the TQDomEntityReference with toEntityReference().

See also toEntityReference().

Reimplemented in TQDomEntityReference.

bool TQDomNode::isNotation () const [virtual]

Returns TRUE if the node is a notation; otherwise returns FALSE.

If this function returns TRUE, it does not imply that this object is a TQDomNotation; you can get the TQDomNotation with toNotation().

See also toNotation().

Reimplemented in TQDomNotation.

bool TQDomNode::isNull () const

Returns TRUE if this node is null (i.e. if it has no type or contents); otherwise returns FALSE.

Example: xml/outliner/outlinetree.cpp.

bool TQDomNode::isProcessingInstruction () const [virtual]

Returns TRUE if the node is a processing instruction; otherwise returns FALSE.

If this function returns TRUE, it does not imply that this object is a TQDomProcessingInstruction; you can get the TQProcessingInstruction with toProcessingInstruction().

See also toProcessingInstruction().

Reimplemented in TQDomProcessingInstruction.

bool TQDomNode::isSupported ( const TQString & feature, const TQString & version ) const [virtual]

Returns TRUE if the DOM implementation implements the feature feature and this feature is supported by this node in the version version; otherwise returns FALSE.

See also TQDomImplementation::hasFeature().

bool TQDomNode::isText () const [virtual]

Returns TRUE if the node is a text node; otherwise returns FALSE.

If this function returns TRUE, it does not imply that this object is a TQDomText; you can get the TQDomText with toText().

See also toText().

Reimplemented in TQDomText.

TQDomNode TQDomNode::lastChild () const [virtual]

Returns the last child of the node. If there is no child node, a null node is returned. Changing the returned node will also change the node in the document tree.

See also firstChild() and childNodes().

TQString TQDomNode::localName () const [virtual]

If the node uses namespaces, this function returns the local name of the node; otherwise it returns TQString::null.

Only nodes of type ElementNode or AttributeNode can have namespaces. A namespace must have been specified at creation time; it is not possible to add a namespace afterwards.

See also prefix(), namespaceURI(), TQDomDocument::createElementNS(), and TQDomDocument::createAttributeNS().

TQDomNode TQDomNode::namedItem ( const TQString & name ) const

Returns the first direct child node for which nodeName() equals name.

If no such direct child exists, a null node is returned.

See also nodeName().

TQString TQDomNode::namespaceURI () const [virtual]

Returns the namespace URI of this node or TQString::null if the node has no namespace URI.

Only nodes of type ElementNode or AttributeNode can have namespaces. A namespace URI must be specified at creation time and cannot be changed later.

See also prefix(), localName(), TQDomDocument::createElementNS(), and TQDomDocument::createAttributeNS().

TQDomNode TQDomNode::nextSibling () const [virtual]

Returns the next sibling in the document tree. Changing the returned node will also change the node in the document tree.

If you have XML like this:

    <h1>Heading</h1>
    <p>The text...</p>
    <h2>Next heading</h2>
    
and this TQDomNode represents the <p> tag, nextSibling() will return the node representing the <h2> tag.

See also previousSibling().

Example: xml/outliner/outlinetree.cpp.

TQString TQDomNode::nodeName () const [virtual]

Returns the name of the node.

The meaning of the name depends on the subclass:

Name Meaning
TQDomAttr The name of the attribute
TQDomCDATASection The string "#cdata-section"
TQDomComment The string "#comment"
TQDomDocument The string "#document"
TQDomDocumentFragment The string "#document-fragment"
TQDomDocumentType The name of the document type
TQDomElement The tag name
TQDomEntity The name of the entity
TQDomEntityReference The name of the referenced entity
TQDomNotation The name of the notation
TQDomProcessingInstruction The target of the processing instruction
TQDomText The string "#text"

See also nodeValue().

Example: xml/outliner/outlinetree.cpp.

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

Returns the type of the node.

See also toAttr(), toCDATASection(), toDocumentFragment(), toDocument(), toDocumentType(), toElement(), toEntityReference(), toText(), toEntity(), toNotation(), toProcessingInstruction(), toCharacterData(), and toComment().

Reimplemented in TQDomDocumentType, TQDomDocument, TQDomDocumentFragment, TQDomCharacterData, TQDomAttr, TQDomElement, TQDomNotation, TQDomEntity, TQDomEntityReference, and TQDomProcessingInstruction.

TQString TQDomNode::nodeValue () const [virtual]

Returns the value of the node.

The meaning of the value depends on the subclass:

Name Meaning
TQDomAttr The attribute value
TQDomCDATASection The content of the CDATA section
TQDomComment The comment
TQDomProcessingInstruction The data of the processing intruction
TQDomText The text

All the other subclasses do not have a node value and will return TQString::null.

See also setNodeValue() and nodeName().

Example: xml/outliner/outlinetree.cpp.

void TQDomNode::normalize () [virtual]

Calling normalize() on an element converts all its children into a standard form. This means that adjacent TQDomText objects will be merged into a single text object (TQDomCDATASection nodes are not merged).

bool TQDomNode::operator!= ( const TQDomNode & n ) const

Returns TRUE if n and this DOM node are not equal; otherwise returns FALSE.

TQDomNode & TQDomNode::operator= ( const TQDomNode & n )

Assigns a copy of n to this DOM node.

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 TQDomNode::operator== ( const TQDomNode & n ) const

Returns TRUE if n and this DOM node are equal; otherwise returns FALSE.

TQDomDocument TQDomNode::ownerDocument () const [virtual]

Returns the document to which this node belongs.

TQDomNode TQDomNode::parentNode () const [virtual]

Returns the parent node. If this node has no parent, a null node is returned (i.e. a node for which isNull() returns TRUE).

TQString TQDomNode::prefix () const [virtual]

Returns the namespace prefix of the node or TQString::null if the node has no namespace prefix.

Only nodes of type ElementNode or AttributeNode can have namespaces. A namespace prefix must be specified at creation time. If a node was created with a namespace prefix, you can change it later with setPrefix().

If you create an element or attribute with TQDomDocument::createElement() or TQDomDocument::createAttribute(), the prefix will be TQString::null. If you use TQDomDocument::createElementNS() or TQDomDocument::createAttributeNS() instead, the prefix will not be TQString::null; but it might be an empty string if the name does not have a prefix.

See also setPrefix(), localName(), namespaceURI(), TQDomDocument::createElementNS(), and TQDomDocument::createAttributeNS().

TQDomNode TQDomNode::previousSibling () const [virtual]

Returns the previous sibling in the document tree. Changing the returned node will also change the node in the document tree.

For example, if you have XML like this:

    <h1>Heading</h1>
    <p>The text...</p>
    <h2>Next heading</h2>
    
and this TQDomNode represents the <p> tag, previousSibling() will return the node representing the <h1> tag.

See also nextSibling().

TQDomNode TQDomNode::removeChild ( const TQDomNode & oldChild ) [virtual]

Removes oldChild from the list of children. oldChild must be a direct child of this node.

Returns a new reference to oldChild on success or a null node on failure.

See also insertBefore(), insertAfter(), replaceChild(), and appendChild().

TQDomNode TQDomNode::replaceChild ( const TQDomNode & newChild, const TQDomNode & oldChild ) [virtual]

Replaces oldChild with newChild. oldChild must be a direct child of this node.

If newChild is the child of another node, it is reparented to this node. If newChild is a child of this node, then its position in the list of children is changed.

If newChild is a TQDomDocumentFragment, then oldChild is replaced by all of the children of the fragment.

Returns a new reference to oldChild on success or a null node an failure.

See also insertBefore(), insertAfter(), removeChild(), and appendChild().

void TQDomNode::save ( TQTextStream & str, int indent ) const

Writes the XML representation of the node and all its children to the stream str. This function uses indent as the amount of space to indent the node.

void TQDomNode::setNodeValue ( const TQString & v ) [virtual]

Sets the node's value to v.

See also nodeValue().

void TQDomNode::setPrefix ( const TQString & pre ) [virtual]

If the node has a namespace prefix, this function changes the namespace prefix of the node to pre. Otherwise this function does nothing.

Only nodes of type ElementNode or AttributeNode can have namespaces. A namespace prefix must have be specified at creation time; it is not possible to add a namespace prefix afterwards.

See also prefix(), localName(), namespaceURI(), TQDomDocument::createElementNS(), and TQDomDocument::createAttributeNS().

TQDomAttr TQDomNode::toAttr ()

Converts a TQDomNode into a TQDomAttr. If the node is not an attribute, the returned object will be null.

See also isAttr().

TQDomCDATASection TQDomNode::toCDATASection ()

Converts a TQDomNode into a TQDomCDATASection. If the node is not a CDATA section, the returned object will be null.

See also isCDATASection().

TQDomCharacterData TQDomNode::toCharacterData ()

Converts a TQDomNode into a TQDomCharacterData. If the node is not a character data node the returned object will be null.

See also isCharacterData().

TQDomComment TQDomNode::toComment ()

Converts a TQDomNode into a TQDomComment. If the node is not a comment the returned object will be null.

See also isComment().

TQDomDocument TQDomNode::toDocument ()

Converts a TQDomNode into a TQDomDocument. If the node is not a document the returned object will be null.

See also isDocument().

TQDomDocumentFragment TQDomNode::toDocumentFragment ()

Converts a TQDomNode into a TQDomDocumentFragment. If the node is not a document fragment the returned object will be null.

See also isDocumentFragment().

TQDomDocumentType TQDomNode::toDocumentType ()

Converts a TQDomNode into a TQDomDocumentType. If the node is not a document type the returned object will be null.

See also isDocumentType().

TQDomElement TQDomNode::toElement ()

Converts a TQDomNode into a TQDomElement. If the node is not an element the returned object will be null.

See also isElement().

Example: xml/outliner/outlinetree.cpp.

TQDomEntity TQDomNode::toEntity ()

Converts a TQDomNode into a TQDomEntity. If the node is not an entity the returned object will be null.

See also isEntity().

TQDomEntityReference TQDomNode::toEntityReference ()

Converts a TQDomNode into a TQDomEntityReference. If the node is not an entity reference, the returned object will be null.

See also isEntityReference().

TQDomNotation TQDomNode::toNotation ()

Converts a TQDomNode into a TQDomNotation. If the node is not a notation the returned object will be null.

See also isNotation().

TQDomProcessingInstruction TQDomNode::toProcessingInstruction ()

Converts a TQDomNode into a TQDomProcessingInstruction. If the node is not a processing instruction the returned object will be null.

See also isProcessingInstruction().

TQDomText TQDomNode::toText ()

Converts a TQDomNode into a TQDomText. If the node is not a text, the returned object will be null.

See also isText().


Related Functions

TQTextStream & operator<< ( TQTextStream & str, const TQDomNode & node )

Writes the XML representation of the node node and all its children to the stream str.


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


Copyright © 2007 TrolltechTrademarks
TQt 3.3.8