2 Replies Latest reply on Sep 19, 2008 7:53 AM by BluDragon777

    Loading External XML

    FlashAmit
      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>

        • 1. Re: Loading External XML
          eonestudio
          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>
          • 2. Re: Loading External XML
            BluDragon777
            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.)