Highlighted

Loading External XML

Avatar

Avatar

FlashAmit

Avatar

FlashAmit

FlashAmit

18-09-2008

I am using code given in the flex help to load a external
xml. But it generated error "Access to undefied property loader."



My mxml file code is as below:





<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="
http://www.adobe.com/2006/mxml"
xmlns="*"

layout="vertical" >



<mx:Script>

<![CDATA[

import flash.events.Event;

import flash.net.URLLoader;

import flash.net.URLRequest;



var externalXML:XML;

var loader:URLLoader = new URLLoader();

var request:URLRequest = new URLRequest("
http://localhost/work/rnd/images.xml");

loader.load(request);

loader.addEventListener(Event.COMPLETE, onComplete);



function onComplete(event:Event):void

{

var loader:URLLoader = event.target as URLLoader;

if (loader != null)

{

externalXML = new XML(loader.data);

xmlText.text = (externalXML.toXMLString());

}

else

{

trace("loader is not a URLLoader!");

}

}

]]>

</mx:Script>

<mx:HBox width="400">

<mx:Label text="xml:"/>

<mx:TextInput id="xmlText" width="100%"
enter="mainTxt.text = myGreeter.sayHello(userNameTxt.text);" />

</mx:HBox>

</mx:Application>



Replies

Highlighted

Avatar

Avatar

eonestudio

Avatar

eonestudio

eonestudio

18-09-2008

Try this... you need to make sure you call the code after the
page is created via creationComplete="init()".



<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="
http://www.adobe.com/2006/mxml"
xmlns="*" layout="vertical" creationComplete="init()">



<mx:Script>

<![CDATA[

import flash.events.Event;

import flash.net.URLLoader;

import flash.net.URLRequest;



public function init():void{

loadXML();

}



public function loadXML():void{



var externalXML:XML;

var loader:URLLoader = new URLLoader();

var request:URLRequest = new URLRequest("
http://localhost/work/rnd/images.xml");

loader.load(request);

loader.addEventListener(Event.COMPLETE, onComplete);



}



function onComplete(event:Event):void

{

var loader:URLLoader = event.target as URLLoader;

if (loader != null)

{

externalXML = new XML(loader.data);

xmlText.text = (externalXML.toXMLString());

}

else

{

trace("loader is not a URLLoader!");

}

}

]]>

</mx:Script>

<mx:HBox width="400">

<mx:Label text="xml:"/>

<mx:TextInput id="xmlText" width="100%"
enter="mainTxt.text = myGreeter.sayHello(userNameTxt.text);" />

</mx:HBox>

</mx:Application>

Highlighted

Avatar

Avatar

BluDragon777

Avatar

BluDragon777

BluDragon777

19-09-2008

I hit this same issue a couple of weeks ago and posted the
same question in the general forum which was answered by Greg
Lafrance with the same answer that eonestudio gave you. (A hint to
search next time.)