Mar
18
2010

 

This post is a continuation of Part 1, which can be found here: http://permadi.com/blog/2010/03/flash-as3-basic-xml-reading-and-parsing-example-part-1/

To make it easier, here’s again the example XML that we are using:

[c]
<?xml version="1.0" standalone="yes"?>
    <news>
         <header>Vacation is Good</header>
         <content>Scientists recommend a full month of nice vacation for people who are bored.</content>
         <info more_info="www.permadi.com">
                <comment>No comment</comment>
                <author>F. Permadi</author>
         </info>
    </news>[/c]

Now let’s start putting these nodes in their intended places. To print the node and node, we can simply refer to it by using E4X notation like this using the node names (remember that we have created several textboxes in Part 1 to hold these nodes):

[c]
function onXMLFileLoaded(e:Event):void
{
	var myXML:XML=new XML(e.target.data);
	header.text=myXML.header;
	content.text=myXML.content;
}
[/c]

The equivalent hard-way to do the same thing is like below:

[c]
function onXMLFileLoaded(e:Event):void
{
	var myXML:XML=new XML(e.target.data);
	header.text=myXML.children()[0];
	content.text=myXML.children()[1];
}
[/c]

The output should be like below, showing the header and content node now containing the XML data:

To acces the author node, we can walk though the hierarchy using E4X notation just like this:
myXML.info.author because author is the child node of info. Or using the hard way: myXML.children()[2].children()[1]. Why children at index 1? Because it’s the second children.

So let’s print the author node:

[c]
function onXMLFileLoaded(e:Event):void
{
	var myXML:XML=new XML(e.target.data);
	header.text=myXML.header;
	content.text=myXML.content;
	author.text=myXML.info.author;
}
[/c]

And the author node should show up, like below:

To print the more_info attribute, we can use the @more_info to denote attribute. Since moreInfo is the attribute of the info node, then the full access path is: myXML.info.@more_info

[c]
function onXMLFileLoaded(e:Event):void
{
	var myXML:XML=new XML(e.target.data);
	header.text=myXML.header;
	content.text=myXML.content;
	author.text=myXML.info.author;
	moreInfo.text=myXML.info.@more_info;
}
[/c]

And the autput is like below:

As a side-node, the hard way to access the more_info is using the attibute() function, like below:
myXML.info.attributes()[0];

FLA file (Requires Flash CS3)
XMLfile (news.xml)

One Response to “Flash AS3: Basic XML Reading And Parsing Example – Part 2”

  1. [...] http://www.permadi.com/blog/2010/03/flash-as3-basic-xml-reading-and-parsing-example-part-2/ [...]