Summary:
See also: Built-in Classes, XML Utils, NodeList
The DomNode class provides methods to manipulate a node of a data tree, following the DOM standards.
om.DomNode
Object Methods | |
Name | Description |
Node creation | |
appendChild( src om.DomNode ) |
Adds a DomNode at the end of the list of children in this node. |
createChild( tag STRING ) RETURNING om.DomNode |
Creates a DomNode and adds it to the children list of this node. |
insertBefore( new om.DomNode, exn om.DomNode ) |
Inserts a DomNode just before the existing node referenced by exn. |
removeChild( node om.DomNode ) |
Removes the child node referenced by node and removes any of its descendents. |
replaceChild( new om.DomNode, old om.DomNode ) |
Replaces the child node referenced by old by the node new. |
In/Out Utilities | |
loadXml( file STRING ) RETURNING om.DomNode |
Creates a new DomNode object by loading an XML file and attaches it to this node as a child. The new created node object is returned from the function. |
writeXml( file STRING ) |
Writes an XML file with the current node. |
write( shd om.SaxDocumentHandler ) |
Outputs an xml-tree to a sax document handler. |
Node identification | |
getId( ) RETURNING INTEGER |
Returns the internal integer identifier automatically assigned to an Abstract User Interface DomNode. Returns -1 for nodes that are not part of the Abstract User Interface tree. |
getTagName( ) RETURNING STRING |
Returns the tag name of the node. |
Attributes management | |
setAttribute( att STRING, val STRING
) |
Sets the attribute att with value val. |
getAttribute( att STRING ) RETURNING STRING |
Returns the value of the attribute having the name att. |
getAttributeInteger(att STRING, def
INTEGER) RETURNING INTEGER |
Returns the value of the attribute having the name att as an integer value. Returns def if the attribute is not defined. |
getAttributeString(att STRING, def
STRING) RETURNING INTEGER |
Returns the value of the attribute having the name att as a string value. Returns def if the attribute is not defined. |
getAttributeName( pos INTEGER ) RETURNING STRING |
Returns the name of the attribute at the position pos (1 = first). |
getAttributesCount() RETURNING INTEGER |
Returns the number of attributes of this DomNode. |
getAttributeValue( pos INTEGER ) RETURNING STRING |
Returns the value of the attribute at the position pos (1 = first). |
removeAttribute( att STRING ) |
Deletes the attribute identified by att. |
Tree navigation | |
getChildCount( ) RETURNING INTEGER |
Returns the number of children. |
getChildByIndex( pos INTEGER ) RETURNING om.DomNode |
Returns the child node at index pos (1 = first). |
getFirstChild( ) RETURNING om.DomNode |
Returns the first child node. |
getLastChild( ) RETURNING om.DomNode |
Returns the last DomNode in the list of children. |
getNext( ) RETURNING om.DomNode |
Returns the next sibling DomNode of this node. |
getParent( ) RETURNING om.DomNode |
Returns the parent DomNode of this node. |
getPrevious( ) RETURNING om.DomNode |
Returns the previous sibling DomNode of this node. |
selectByTagName( name STRING ) RETURNING om.NodeList |
Creates a list of nodes by recursively searching nodes by tag name. |
selectByPath( path STRING ) RETURNING om.NodeList |
Creates a list of nodes by recursively searching nodes matching an XPath-like pattern. |
A DomNode object can have attributes with values, except if it is a text node. In this case, you can only get/set the text of the node, since text nodes cannot have attributes.
The DomNode class provides a complete set of methods to modify attribute values, like the DomNode.getAttribute() or DomNode.setAttribute() methods.
To create a text node, use the DomDocument.createChars() method.
A DomNode object can have zero or more DomNode children that can have, in turn, other children.
The DomNode class provides a complete set of methods to manipulate DomNode child objects, like DomNode.getFirstChild(), for example.
To get the tag name of the DOM node, use the DomNode.getTagName() method.
The DomNode class provides the DomNode.writeXml() method to save the DOM tree into a file in XML format.The DomNode.selectByTagName() and DomNode.selectByPath() methods allow to search for children nodes according to a tag name (i.e. a type of node) or by using an XPath-like search criteria. See the NodeList class for more details.
To create a DOM tree with the following structure (represented in XML format):
<Vehicles> <Car name="Corolla" color="Blue" weight="1546">Nice car! </Car> <Bus name="Maxibus" color="Yellow" weight="5278"> <Wheel width="315" diameter="925" /> <Wheel width="315" diameter="925" /> <Wheel width="315" diameter="925" /> <Wheel width="315" diameter="925" /> </Bus> </Vehicles>
You write the following:
01
MAIN02
DEFINE d om.DomDocument03
DEFINE r, n, t, w om.DomNode04
DEFINE i INTEGER05
06
LET d = om.DomDocument.create("Vehicles")07
LET r = d.getDocumentElement()08
09
LET n = r.createChild("Car")10
CALL n.setAttribute("name","Corolla")11
CALL n.setAttribute("color","Blue")12
CALL n.setAttribute("weight","1546")13
14
LET t = d.createChars("Nice car!")15
CALL n.appendChild(t)16
17
LET n = r.createChild("Bus")18
CALL n.setAttribute("name","Maxibus")19
CALL n.setAttribute("color","yellow")20
CALL n.setAttribute("weight","5278")21
FOR i=1 TO 422
LET w = n.createChild("Wheel")23
CALL w.setAttribute("width","315")24
CALL w.setAttribute("diameter","925")25
END FOR26
27
CALL r.writeXml("Vehicles.xml")28
29
END MAIN
The following example displays a DOM tree content recursively:
01
FUNCTION displayDomNode(n,e)02
DEFINE n om.DomNode03
DEFINE e, i, s INTEGER04
05
LET s = e*206
DISPLAY s SPACES || "Tag: " || n.getTagName()07
08
DISPLAY s SPACES || "Attributes:"09
FOR i=1 TO n.getAttributesCount()10
DISPLAY s SPACES || " " || n.getAttributeName(i) || "=[" || n.getAttributeValue(i) ||"]"11
END FOR12
LET n = n.getFirstChild()13
14
DISPLAY s SPACES || "Child Nodes:"15
WHILE n IS NOT NULL16
CALL displayDomNode(n,e+1)17
LET n = n.getNext()18
END WHILE19
20
END FUNCTION
The following example outputs a Dom tree without indentation.
01
MAIN02
DEFINE d om.DomDocument03
DEFINE r, n, t, w om.DomNode04
DEFINE dh om.SaxDocumentHandler05
06
DEFINE i INTEGER07
08
LET dh = om.XmlWriter.createPipeWriter("cat")09
CALL dh.setIndent(FALSE)10
11
LET d = om.DomDocument.create("Vehicles")12
LET r = d.getDocumentElement()13
14
LET n = r.createChild("Car")15
CALL n.setAttribute("name","Corolla")16
CALL n.setAttribute("color","Blue")17
CALL n.setAttribute("weight","1546")18
19
LET t = d.createChars("Nice car!")20
CALL n.appendChild(t)21
22
LET n = r.createChild("Bus")23
CALL n.setAttribute("name","Maxibus")24
CALL n.setAttribute("color","yellow")25
CALL n.setAttribute("weight","5278")26
FOR i=1 TO 427
LET w = n.createChild("Wheel")28
CALL w.setAttribute("width","315")29
CALL w.setAttribute("diameter","925")30
END FOR31
32
CALL r.write(dh)33
34
END MAIN