Tuesday, October 22, 2013

Use XML in Flash AS3

I thought you you already know what is XML.

Now i have a project where i need to get product id and website URL from XML file.

Your have a xml file productinfo.xml
 
<?xml version="1.0" encoding="UTF-8" ?>
<product>
<purl>http://www.somedoamin.com/myproduct</purl> 
<pid>productid</pid>
</product>

AS3 codes:

var xmlLoader:URLLoader = new URLLoader();
var url:URLRequest = new URLRequest("productinfo.xml");
xmlLoader.load(url);
xmlLoader.addEventListener(Event.COMPLETE, onXmlLoad);
function onXmlLoad(e:Event):void {
  
xml = new XML(xmlLoader.data);
trace(xmlLoader.data); //to see xml file.

for each (var item:XML in xml.elements()) {
   
    if(item.name()=='purl')
    {
        websiteurl=item;
trace(item);
    }
    if(item.name()=='pid')
       {
           myproid=item;
trace(item);
           
       }

               
            }

}

==================================