| An example using the XML file shown above
In the example below, the
function that is called when the xml file has been loaded does a trace on various
node properties, child nodes, and properties of those child nodes. Comparing the
output below with the file above and looking at the code should give you some
idea of how various read functions of the XML object work. #include
"xmlnitro2.as" function myLoad () { // This function gets
executed when Flash has // loaded the complete XML file. It displays //
node properties for the first 4 levels of // nodes found in the code,
this = the XML // object that called this function // (eg this.firstChild
= the first child node of thisXML) trace("xml declaration: "
+ this.xmlDecl); trace("xml doctype: " + this.docTypeDecl); trace("number
of articles: " + this.firstChild.childNodes.length); trace("
"); level1Child = this.firstChild; level2Child = level1Child.firstChild; level3Child
= level2Child.firstChild; level4Child = level3Child.firstChild; //
note that this last statement could also have been written // level4Child
= this.firstChild.firstChild.firstChild.firstChild; // or //
level4Child = // this.childNodes[0].childNodes[0].childNodes[0].childNodes[0]; trace("level1:
name=" + level1Child.nodeName); trace(" value=" + level1Child.nodeValue); trace("
type=" + level1Child.nodeType); trace(" "); trace("level2:
name=" + level2Child.nodeName); trace(" value=" + level2Child.nodeValue); trace("
type=" + level2Child.nodeType); // the attributes property is itself
an object for (attr in level2Child.attributes) { trace("
attributes=" + attr + " value=" + level2Child.attributes[attr]); } trace("
"); trace("level3: name=" + level3Child.nodeName); trace("
value=" + level3Child.nodeValue); trace(" type=" + level3Child.nodeType); //
the following will not produce any output // because level3Child has
no attributes for (attr in level3Child.attributes) { trace("
attributes=" + attr + " value=" + level3Child.attributes[attr]); } trace("
"); trace("level4: name=" + level4Child.nodeName); trace("
value=" + level4Child.nodeValue); trace(" type=" + level4Child.nodeType); trace("
"); trace(level3Child.nextSibling.nodeName); trace(level3Child.nextSibling.firstChild.nodeValue); trace("
"); n3 = level2Child.childNodes.length; trace("number of level3
nodes = " + n3); trace(" 1st level3 node = " + level2Child.childNodes[0].nodeName); trace("
2nd level3 node = " + level2Child.childNodes[1].nodeName); trace("
3rd level3 node = " + level2Child.childNodes[2].nodeName); trace("
last level3 node = " + level2Child.childNodes[n3-1].nodeName); } thisXML
= new XML(); thisXML.ignoreWhite = true; thisXML.onLoad = myLoad; thisXML.load("environment_page.xml"); The
output from the code above follows. Notice that even text between tags is treated
as a node. It is a node with nodeType = 3 and its value is contained in
the XML property nodeValue. The nodeName property is meaningless
(=null) for these nodes. Nodes which look like tags, on the other hand,
have a nodeValue of null, a nodeName which is the tag itself, and are nodeType
1, as shown in the output below. xml declaration: <?xml version="1.0"
encoding="iso-8859-1"?> xml doctype: <!DOCTYPE moreovernews
SYSTEM "http://p.moreover.com/xml_dtds/moreovernews.dtd"> number
of articles: 20 level1: name=moreovernews value=null type=1
level2: name=article value=null type=1 attributes=id value=_22212934
level3: name=url value=null type=1 level4: name=null
value=http://c.moreover.com/click/here.pl?x22212932 type=3 headline_text
Green Party supports logging venture number of level3 nodes = 10
1st level3 node = url 2nd level3 node = headline_text 3rd level3 node
= source last level3 node = access_status To try this for yourself,
grab one of the moreover.com news files from the link above. Get Branden's xmlnitro2.as
routine. Cut and paste the code above into the Frame Actions for frame 1 of a
blank movie, changing the filename in the thisXML.load statement appropriately,
and give it a try! Then modify as you like. |