Skip to main content
April 1, 2008
Question

reading XML in AS 3.0

  • April 1, 2008
  • 1 reply
  • 281 views
Hello, i have some trouble finding information in the 'migration section' about the replacement command's
for reading XML in actionscript 3.0.

For example, the firstChild, childNodes commands don't work anymore.
I find it hard to understand the help files about xml and don't know how to do it the proper way.


If i would read this xml (for building a drop down menu) example from kirupa.com
<?xml version="1.0"?>
<menu name="links">
<menu name="macromedia">
<item name="flash" action="gotoURL" variables=" http://www.macromedia.com/software/flash"/>
<item name="dreamweaver" action="gotoURL" variables=" http://www.macromedia.com/software/dreamweaver"/>
<item name="fireworks" action="gotoURL" variables=" http://www.macromedia.com/software/fireworks"/>
</menu>
<menu name="kirupa">
<item name="home" action="gotoURL" variables=" http://www.kirupa.com"/>
<item name="forums" action="gotoURL" variables=" http://www.kirupaforum.com/forums/index.php"/>
<menu name="tutorials">
<item name="actionscript" action="gotoURL" variables=" http://www.kirupa.com/developer/actionscript/index.htm"/>
<item name="photoshop" action="gotoURL" variables=" http://www.kirupa.com/photoshop/index.htm"/>
<item name="web" action="gotoURL" variables=" http://www.kirupa.com/web/index.htm"/>
</menu>
</menu>
<item name="google" action="gotoURL" variables=" http://www.google.com"/>
</menu>

I have this so far..:

var myXML:XML = new XML();
var XML_URL:String = "menu1.xml";
var myXMLURL:URLRequest = new URLRequest(XML_URL);
var myLoader:URLLoader = new URLLoader(myXMLURL);
myLoader.addEventListener("complete", xmlLoaded);

function xmlLoaded(event:Event):void
{
myXML = XML(myLoader.data);

trace(myXML.children());

trace("Data loaded.");
}


Is there some more clear information about the new way of reading XML ?

Or do i also need to rewrite the xml code to a different format?

This topic has been closed for replies.

1 reply

Craig Grummitt
Inspiring
April 2, 2008
if you want to use the legacy(AS2) XML class for interpreting your XML, it has been renamed XMLDocument in AS3. so you could use:

var myXML:XMLDocument=XMLDocument(myLoader.data);

and continue to use your firstChild, childNodes etc.

But if you want to try the AS3 methods for interpreting XML, called E4X, use the XML class. You'll find E4X much more intuitive and easier to use.

The structure of XML hasn't changed at all, just the way Flash deals with it, so to answer your question, you do not need to rewrite your xml code to a different format.

That said, however, you do need to look at your specific xml code as it isn't well formed. the last two tags are outside of the root tag. i've added indentation below to make this clear.

The help is quite thorough on the new xml classes - read the section under Programming ActionScript 3.0, Working with XML, and if you've still got issues, feel free to post specific questions.