
Using XML document nodes
423
Using this XML document as a starting point, the following examples demonstrate how to use
these script terms to access the data within various node levels of the XML structure.
The XML looks like the following example:
<?xml version="1.0"?>
<e1>
<tagName attr1="val1" attr2="val2"/>
<e2>element 2</e2>
<e3>element 3</e3>
Here is some text
</e1>
The following script returns the name of the first XML tag:
put gParserObject.child[1].name
-- "e1"
The
gParserObject
variable contains the parsed XML. When used in the preceding script, it
refers to the root node of the XML document. The script term
child[1]
refers to the first level of
nested tag, which is the
e1
tag.
To find out what kind of node the first tag is, use the
type
method, as shown in the following
example:
put gParserObject.child[1].type
-- #element
To refer to nodes that are nested more than one level deep, use more than one level of child
reference. The following script returns the name of the first tag that is nested within the
e1
tag:
put gParserObject.child[1].child[1].name
-- "tagName"
The following script returns the name of the second tag that is nested within the
e1
tag:
put gParserObject.child[1].child[2].name
-- "e2"
To refer to text data that occurs within a particular tag, use the
text
property. The text is a child
node of the tag that contains it, so you need an additional level of child reference. This script
returns the following string, which appears inside the
e2
tag from the previous XML example:
put gParserObject.child[1].child[2].child[1].text
-- "element 2"
In this example, the
gParserObject
variable refers to the root node of the XML. The
child[1]
refers to the
e1
tag, which occupies the first level down in the XML’s nested structure. The
child[2]
refers to the second tag within the
e1
tag, which is the
e2
tag. The last
child[1]
refers
to the text within the
e2
tag, which is
element 2
. Finally, the
text
property is specified, so script
returns the text of the node rather than any other property of the node.
The fourth child of the
e1
tag is a line of text that reads
here is some text
. This text is a child
the same as the XML tags that precede it. You can get the type of this child the same way you get
other children.
The following script returns the type of the fourth child of the
e1
tag:
put gParserObject.child[1].child[4].type
-- #text
Summary of Contents for DIRECTOR MX 2004-USING DIRECTOR
Page 1: ...DIRECTOR MX 2004 Using Director...
Page 16: ...16 Chapter 1 Introduction...
Page 82: ...82 Chapter 3 Sprites...
Page 98: ...98 Chapter 4 Animation...
Page 134: ...134 Chapter 5 Bitmaps...
Page 242: ...242 Chapter 10 Sound and Synchronization...
Page 274: ...274 Chapter 11 Using Digital Video...
Page 290: ...290 Chapter 12 Behaviors...
Page 302: ...302 Chapter 13 Navigation and User Interaction...
Page 334: ...334 Chapter 15 The 3D Cast Member 3D Text and 3D Behaviors...
Page 392: ...392 Chapter 16 Working with Models and Model Resources...
Page 418: ...418 Chapter 18 Movies in a Window...
Page 446: ...446 Chapter 22 Managing and Testing Director Projects...