Summary:
See also: The Genero Web Services XML Library
The DomNode class provides methods to manipulate a node of a DomDocument object. You can create a DomNode object using creation methods in the DomDocument class. Notice that status is set to zero after a successful method call.
Syntax
xml.DomNode
Object Methods | |
Name | Description |
getParentNode() |
Returns the parent DomNode object
for this DomNode object, or NULL. In the case of a DomDocument node,
this method will return NULL (parent is not a DomNode object) but isAttached()
will return TRUE. |
getFirstChild() |
Returns the first child DomNode object for this XML Element DomNode object, or NULL. |
getFirstChildElement() |
Returns the first XML Element child DomNode object for this DomNode object, or NULL. (Not part of W3C API). |
getLastChild() |
Returns the last child DomNode object for this XML Element DomNode object, or NULL. |
getLastChildElement() |
Returns the last child XML element DomNode object for this DomNode object, or NULL. (Not part of W3C API). |
getNextSibling() |
Returns the DomNode object immediately following this DomNode object, or NULL. |
getNextSiblingElement() |
Returns the XML Element DomNode object immediately following this DomNode object, or NULL. (Not part of W3C API). |
getPreviousSibling() |
Returns the DomNode object immediately preceding this DomNode object, or NULL. |
getPreviousSiblingElement() |
Returns the XML Element DomNode object immediately preceding this DomNode object, or NULL. (Not part of W3C API). |
getOwnerDocument() |
Returns the DomDocument object containing this DomNode object, or NULL. |
hasChildNodes() |
Returns TRUE if this node has child nodes; otherwise, returns FALSE. |
getChildrenCount() |
Returns the number of child DomNode objects for this DomNode object. |
getChildNodeItem( |
Returns the child DomNode object at
a given position for this DomNode object. Throws an exception in case of errors, and updates status with an error code. |
Object Methods | |
Name | Description |
prependChildElement( |
Creates and adds a child XML Element
node to the beginning of the list of child nodes for this XML Element
DomNode object (Not part of W3C API); name
is the name of the XML element to add. Returns the XML Element DomNode
object, or NULL. Throws an exception in case of errors, and updates status with an error code. |
prependChildElementNS( |
Creates and adds a child
namespace-qualified
XML Element node to the beginning of the list of child nodes for this
XML Element DomNode object (Not part of W3C API); name
is the name of the XML Element to add; ns is the
namespace URI of the XML Element to add. Returns the XML
Element DomNode object, or NULL. Throws an exception in case of errors, and updates status with an error code. |
addPreviousSibling( |
Adds a DomNode object as the
previous sibling of this DomNode object (Not part of W3C API);
node is the node to add. The DomNode object node must be the child of an element or document node; otherwise, the operation fails. Throws an exception in case of errors, and updates status with an error code. |
addNextSibling( |
Adds a DomNode object as the next
sibling of this DomNode object (Not part of W3C API);
node is the node to add. The DomNode object node must be the child of an element or document node; otherwise, the operation fails. Throws an exception in case of errors, and updates status with an error code. |
prependChild( |
Adds a child DomNode object to the
beginning of the child list for this DomNode object (Not part
of W3C API); node is the node to add. The DomNode object node must be the child of an element or document node; otherwise, the operation fails. Throws an exception in case of errors, and updates status with an error code. |
appendChild( |
Adds a child DomNode object to the
end of the child list for this DomNode object; node
is the node to add. The DomNode object node must be the child of an element or document node; otherwise, the operation fails. Throws an exception in case of errors, and updates status with an error code. |
insertBeforeChild( |
Inserts a DomNode object before an
existing child DomNode object; node is the node to
insert, ref is the reference node - the node before
which the new node must be inserted. Throws an exception in case of errors, and updates status with an error code. |
insertAfterChild( |
Inserts a DomNode object after an
existing child DomNode object (Not part of W3C API);
node is the node to insert, ref
is the reference node - the node after which the new node must be
inserted. Throws an exception in case of errors, and updates status with an error code. |
removeChild( |
Removes a child DomNode object from
the list of child DomNode objects, where node is
the node to remove. Throws an exception in case of errors, and updates status with an error code. |
removeAllChildren() |
Removes all child DomNode objects
from this DomNode object Throws an exception in case of errors, and updates status with an error code. |
replaceChild( |
Replaces an existing child DomNode
with another child DomNode object, where old is
the child to be replaced and new is the replacement
child. Throws an exception in case of errors, and updates status with an error code. |
appendChildElement( |
Creates and adds a child XML Element
node to the end of the list of child nodes for this XML Element DomNode
object (Not part of W3C API); name
is the XML Element name. Returns the XML Element DomNode object, or
NULL. Throws an exception in case of errors, and updates status with an error code. |
appendChildElementNS( |
Creates and adds a child namespace
qualified
XML Element node to the end of the list of child nodes for this XML
Element DomNode object (Not part of W3C API); prefix
is the prefix of the XML Element to add; name
is the name of the XML Element to add; ns
is the namespace URI of the XML Element to add. Returns the
XML Element DomNode object, or NULL. Throws an exception in case of errors, and updates status with an error code. |
clone( |
Returns a duplicate DomNode object
of this node. If deep is TRUE, child DomNode
objects are cloned too; otherwise, only the DomNode itself is cloned.
Returns a copy of this DomNode object, or NULL.* Throws an exception in case of errors, and updates status with an error code. |
Object Methods | |
Name | Description |
getNodeType() |
Gets the XML type for this DomNode
object; returns one of the XML DomNode
types, or NULL. |
getLocalName() |
Gets the local name for this DomNode object. If DomNode has a qualified name, only the local part is returned. |
getNodeName() |
Gets the name for this DomNode object; returns the qualified name of this DomNode object, or NULL. If DomNode does not have a qualified name, the local part is returned. |
getNamespaceURI() |
Returns the namespace URI for this DomNode object, or NULL. |
getNodeValue() |
Returns the value for this DomNode object, or NULL. |
getPrefix() |
Returns the prefix for this DomNode object, or NULL. |
isAttached() |
Returns whether the node is attached to the XML document (Not part of W3C API). Returns TRUE if this DomNode object is attached to a DomDocument object as a child and was not removed later on; otherwise it returns FALSE. |
Object Methods | |
Name | Description |
setNodeValue( |
Sets the node
value for this DomNode object, where val is the
node value. Throws an exception in case of errors, and updates status with an error code. |
setPrefix( |
Sets the prefix
for this DomNode object. This method is only valid on namespace qualified Element or Attribute nodes. Throws an exception in case of errors, and updates status with an error code. |
toString()
|
Returns a string representation of
this DomNode object, or NULL (Not part of W3C API).
Throws an exception in case of errors, and updates status with an error code. |
Object Methods | |
Name | Description |
hasAttribute( |
Checks whether this XML
Element DomNode object has the XML Attribute specified by name.
Returns TRUE if an XML Attribute with the given name is carried by this
XML Element DomNode object; otherwise, returns FALSE. Throws an exception in case of errors, and updates status with an error code. |
hasAttributeNS( |
Checks whether a namespace qualified
XML Attribute of a given name is carried by this XML Element DomNode
object, where name the name of the XML Attribute to check ns: the
namespace URI of the XML Attribute to check Returns TRUE if an XML
Attribute with the given name and namespace URI is carried by this XML
Element DomNode object, FALSE otherwise. Throws an exception in case of errors, and updates status with an error code. |
getAttributeNode( |
Returns an XML Attribute DomNode
object for this XML Element DomNode object, or NULL; name
is the name of the attribute to retrieve. Throws an exception in case of errors, and updates status with an error code. |
getAttributeNodeNS( |
Returns a namespace-qualified XML
Attribute DomNode object for this XML Element DomNode object, or NULL; name
is the name of the XML Attribute to retrieve and ns
is the namespace URI of the XML Attribute to retrieve. Throws an exception in case of errors, and updates status with an error code. |
setAttributeNode( |
Sets (or resets) an XML Attribute
DomNode object to an XML Element DomNode object, , where node
is the XML Attribute DomNode object to set. Throws an exception in case of errors, and updates status with an error code. |
setAttributeNodeNS( |
Sets (or resets) a
namespace-qualified XML Attribute DomNode object to an XML Element
DomNode object, where node is the XML Attribute
DomNode object to set. Throws an exception in case of errors, and updates status with an error code. |
getAttribute( |
Returns the value of an XML
Attribute for this XML Element DomNode object, where name
is the name of the XML attribute to retrieve; returns the XML Attribute
value, or NULL. Throws an exception in case of errors, and updates status with an error code. |
getAttributeNS( |
Returns the value of a namespace
qualified XML Attribute for this XML Element DomNode object, where name
is the name and ns is the namespace URI of the XML
Attribute to retrieve; returns the XML Attribute value, or NULL. Throws an exception in case of errors, and updates status with an error code. |
setAttribute( |
Sets (or resets) an XML Attribute
for this XML Element DomNode object, where name is
the name of the XML Attribute and val is the value
of the XML Attribute. Throws an exception in case of errors, and updates status with an error code. |
setAttributeNS( |
Sets (or resets) a
namespace-qualified XML Attribute for this XML Element DomNode object,
where prefix is the prefix of the XML
Attribute, name is the name of the XML
Attribute, ns is the namespace URI of the XML
Attribute, and val is the value of the
XML Attribute. Throws an exception in case of errors, and updates status with an error code. |
setIdAttribute( |
Declare (or undeclare) the XML
Attribute of given name to be of type ID. Use the value TRUE for the
parameter isID to declare that attribute for being
a user-determined ID attribute, false otherwise. This affects the behavior of getElementById. Throws an exception in case of errors, and updates status with an error code. |
setIdAttributeNS( |
Declare (or undeclare) the
namespace-qualified XML Attribute of given name and namespace to be of
type ID. Use the value TRUE for the parameter isID
to declare that attribute for being a user-determined ID attribute,
false otherwise. This affects the behavior of getElementById. Throws an exception in case of errors, and updates status with an error code. |
removeAttribute( |
Removes an XML Attribute for this
XML Element DomNode object, where name is the name
of the XML attribute to remove. Status is updated with an error code. Throws an exception in case of errors, and updates status with an error code. |
removeAttributeNS( |
Removes a namespace qualified XML
Attribute for this XML Element DomNode object, where name
is the name and ns is the namespace URI
of the XML Attribute to remove. Throws an exception in case of errors, and updates status with an error code. |
hasAttributes() |
Returns TRUE if this node has XML Attribute nodes; otherwise, returns FALSE. |
getAttributesCount() |
Returns the number of XML Attribute
DomNode objects on this XML Element DomNode object. Throws an exception in case of errors, and updates status with an error code. |
getAttributeNodeItem( |
Returns the XML Attribute DomNode
object at a given position on this XML Element DomNode object, where pos
is the position of the node to return (Index starts at 1). Returns the
XML Attribute DomNode object at the given position, or NULL. Throws an exception in case of errors, and updates status with an error code. |
Object Methods | |
Name | Description |
selectByXPath( |
Returns a DomNodeList object containing all DomNode objects matching an XPath 1.0 expression (Not part of W3C API); expr is the XPath 1.0 expression, NamespacesList is a list of prefixes bounded to namespaces in order to resolve qualified names in the XPath expression. This list must be filled with an even number of arguments, representing the prefix and it corresponding namespace. Examples:
selectByXPath("../../d:Record/*[last()]", "d",
"http://defaultnamespace")
selectByXPath("ns:Record", NULL)
selectByXPath("ns1:Records/ns2:Record", "ns1",
"http://namespace1", "ns2", "http://namespace2")
If the namespaces list is NULL, the prefixes and namespaces defined in the document itself are used if available. A namespace must be an absolute URI (ex 'http://', 'file://'). Throws an exception in case of errors, and updates status with an error code. |
getElementsByTagName( |
Returns a DomNodeList object
containing all XML Element DomNode objects with the same tag name, or
NULL; name is the name of the XML Element tag to
match, or "*" to match all tags. Throws an exception in case of errors, and updates status with an error code. |
getElementsByTagNameNS( |
Returns a DomNodeList object
containing all namespace-qualified XML Element DomNode objects with the
same tag name and namespace, OR NULL. name
is the name of the XML Element tag to match, or "*" to match all tags; ns
is the namespace URI of the XML Element tag to match,
or "*" to match any namespace. Throws an exception in case of errors, and updates status with an error code. |
isDefaultNamespace( |
Checks whether the specified
namespace URI is the default namespace, where ns is
the namespace URI to look for. Returns TRUE if the given namespace is
the default namespace, FALSE otherwise. Throws an exception in case of errors, and updates status with an error code. |
lookupNamespaceURI( |
Looks up the namespace URI
associated to a prefix, starting from this node, where prefix
is the prefix to look for; if NULL, the default
namespace URI will be returned. Returns a namespace URI, or NULL. Throws an exception in case of errors, and updates status with an error code. |
lookupPrefix( |
Looks up the prefix associated to a
namespace URI, starting from this node, where ns is
the namespace URI to look for. Returns the prefix associated to this
namespace URI, or NULL. Throws an exception in case of errors, and updates status with an error code. |
Usage
The getElementsByTagName
and getElementsByTagNameNS
methods return a
DomNodeList
object, unlike the other methods that return a DomNode object. The
DomNodeList is restricted to contain objects with the same tag name
and/or namespace.
Type | Description |
ELEMENT_NODE | The DomNode is an XML Element node. |
ATTRIBUTE_NODE | The DomNode is an XML Attribute node. |
TEXT_NODE | The DomNode is an XML Text node. |
CDATA_SECTION_NODE | The DomNode is an XML CData Section node. |
ENTITY_REFERENCE_NODE | The DomNode is an XML Entity Reference node. |
PROCESSING_INSTRUCTION_NODE | The DomNode is an XML Processing Instruction node. |
COMMENT_NODE | The DomNode is an XML Comment node. |
DOCUMENT_TYPE_NODE | The DomNode is an XML DTD node. |
DOCUMENT_FRAGMENT_NODE | The DomNode is an XML Document Fragment node. |
Following code counts the number of nodes of each type :
01
IMPORT XML02
03
DEFINE nbElt INTEGER04
DEFINE nbAttr INTEGER05
DEFINE nbComment INTEGER06
DEFINE nbPI INTEGER07
DEFINE nbTxt INTEGER08
DEFINE nbCData INTEGER09
MAIN10
DEFINE document xml.DomDocument11
DEFINE ind INTEGER12
# Handle arguments13
IF num_args() !=1 THEN14
CALL ExitHelp()15
END IF16
# Create document, load it, and count the nodes17
LET document = xml.DomDocument.Create()18
CALL document.load(arg_val(1))19
CALL CountDoc(document)20
# Display result21
DISPLAY "Results: "22
DISPLAY " Elements: ",nbElt23
DISPLAY " Attributes:",nbAttr24
DISPLAY " Comments: ",nbComment25
DISPLAY " PI: ",nbPI26
DISPLAY " Texts: ",nbTxt27
DISPLAY " CData: ",nbCData28
END MAIN29
30
FUNCTION CountDoc(d)31
DEFINE d xml.DomDocument32
DEFINE n xml.DomNode33
LET n = d.getFirstDocumentNode()34
WHILE (n IS NOT NULL )35
CALL Count(n)36
LET n = n.getNextSibling()37
END WHILE38
END FUNCTION39
40
FUNCTION Count(n)41
DEFINE n xml.DomNode42
DEFINE child xml.DomNode43
DEFINE next xml.DomNode44
DEFINE node xml.DomNode45
DEFINE ind INTEGER46
DEFINE name STRING47
IF n IS NOT NULL THEN48
IF n.getNodeType() == "COMMENT_NODE" THEN49
LET nbComment = nbComment + 150
END IF51
IF n.getNodeType() == "ATTRIBUTE_NODE" THEN52
LET nbAttr = nbAttr + 153
END IF54
IF n.getNodeType() == "PROCESSING_INSTRUCTION_NODE " THEN55
LET nbPI = nbPI + 156
END IF57
IF n.getNodeType() == "ELEMENT_NODE" THEN58
LET nbElt = nbElt + 159
END IF60
IF n.getNodeType() == "TEXT_NODE" THEN61
LET nbTxt = nbTxt +162
END IF63
IF n.getNodeType() == "CDATA_SECTION_NODE" THEN64
LET nbCData = nbCData + 165
END IF66
IF n.hasChildNodes() THEN67
LET name = n.getLocalName()68
LET child = n.getFirstChild()69
WHILE (child IS NOT NULL )70
CALL Count(child)71
LET child = child.getNextSibling()72
END WHILE73
END IF74
IF n.hasAttributes() THEN75
FOR ind = 1 TO n.getAttributesCount()76
LET node = n.getAttributeNodeItem(ind)77
CALL Count(node)78
END FOR79
END IF80
END IF81
END FUNCTION82
83
FUNCTION ExitHelp()84
DISPLAY "DomCount <xml>"85
EXIT PROGRAM86
END FUNCTION