Monday, May 5, 2008

AS3 basic XML use

// we use URLLoader instead of XML.load();
var loader:URLLoader = new URLLoader();

loader.addEventListener(Event.COMPLETE, loadXML);
loader.load(new URLRequest("example.xml"));

function loadXML(myEvent:Event):void {
        var xml:XML = new XML(myEvent.target.data);
                // you can now traverse the xml object using the actual tag names
                trace(xml.tagname.anothertagname.text());
                
                // you can also target all tags of a certain name
                // with the .. "descendent accessor"
                trace(xml..anothertagname.length());

                // and we use the @ accessor for attributes
                trace(xml.tagname.anothertagname.@myattribute);
                //
}

// here is some xml for your example.xml file

/*

<?xml version="1.0" encoding="UTF-8" ?>
<xml>
<tagname>
<anothertagname myattribute="my attribute goes here">my text goes here</anothertagname>
</tagname>
</xml>

*/

No comments: