User Guide

424 Chapter 19: Using the XML Parser Xtra
This script returns the text of the fourth child of the e1 tag, as shown in the following example:
put gParserObject.child[1].child[4].text
-- "
here is some text
"
The text element includes the white space for Return, Space, and Tab characters as well as the
string
"here is some text".
You can use the script
count method to determine the number of children that exist at a
particular level of the XML structure. The following script returns the number of children at the
2nd level in the previous XML example:
put gparser.child[1].child.count
-- 4
Accessing attributes
Some XML tags contain attributes with values. Use the attributeName and attributeValue
properties to access the attributes of tags that have values. In the previous XML example, the first
tag nested inside the
e1 tag is called tagName and has two attributes called attr1 and attr2.
The following script uses the
attributeName property to return the name of the first attribute of
the tag called
tagName, which is the first child of the e1 tag:
put gParserObject.child[1].child[1].attributeName[1]
-- "attr1"
The following script uses the attributeValue property with an integer to return the value of the
first attribute of the
tagName tag:
put gParserObject.child[1].child[1].attributeValue[1]
-- "val1"
The following script uses the attributeValue property with a string to return the value of the
attr1 attribute:
put gParserObject.child[1].child[1].attributeValue["attr1"]
-- "val1"
The following script uses the count method with the attributeName property to return the
number of attributes in the first child of the
e1 tag:
put gParserObject.child[1].child[1].attributeName.count
-- 2
Parser objects and XML nodes
As described in earlier sections, the parser object in the gParserObject variable stores the root
of the parsed tree of the XML document. An XML node is a node within the tree. The root
node is like an XML node because almost all the operations on XML nodes can be applied to
the root node.
In the previous XML example, the root of the tree is an XML element node named "ROOT OF
XML DOCUMENT" that has no attributes and one child (the
e1 tag). You can get the same
information about the root node as for any of the child nodes.