Monday, December 21, 2009

Load and read XML in AS3

Task: You need to load and read an XML file.

onXMLLoad(event:Event):void
{
var xml:XML = new XML(event.target.data);
trace(xml);
trace("Number of Contacts : " + xml..person.length());
trace("First contact’s favorite food : " + xml.contacts.person[0].favoriteFood);
}
var loader:URLLoader = new URLLoader();
var url:URLRequest = new URLRequest("contacts.xml");
loader.addEventListener(Event.COMPLETE, onXMLLoad);
loader.load(url);

Note: examples, use the following XML contained in a file called contacts.xml:

<xml>
<contacts>
<person>
<name>Mike Chambers</name>
<favoriteFood>Bacon</favoriteFood>
</person>
<person>
<name>John Doe</name>
<favoriteFood>Pez</favoriteFood>
</person>
</contacts>
</xml>


No comments:

Post a Comment