4 Replies Latest reply on Jun 11, 2010 11:04 AM by FlashGuruWiz

    Loading XML files into flex application

    sensomage5

          Hi i'm new to flex and am trying to load a xml file that is currently located on my C drive in  "C:/Listener".  I'm having some issues on how to load it to my application.  Right now  i have this. (filename is equal to the name of my file.  In this case it is AuditTrail.XML)

       

       

      public function XMLload(filename:String):void { 

         var Data:XML = new XML();

         urlSource1 = "Listener/" + filename;    

         var xmlFileReq:URLRequest = new URLRequest(urlSource1);

         xmlLoader1 = new URLLoader(xmlFileReq);

         xmlLoader1.dataFormat = TEXTdataFormat;

         xmlLoader1.addEventListener(Event.COMPLETE,xmlLoadComplete1);

        xmlLoader1.addEventListener(IOErrorEvent.IO_ERROR, xmlIOErrorHandler1);

         xmlLoader1.load(xmlFileReq);

         }

         public function xmlLoadComplete1(event:Event):void {

         Data = XML(xmlLoader1.data);

         trace("loading xml list");

         xmlLoader1.removeEventListener(Event.COMPLETE,xmlLoadComplete1);

        xmlLoader1.removeEventListener(IOErrorEvent.IO_ERROR, xmlIOErrorHandler1);

         }

      public function xmlIOErrorHandler1(event:IOErrorEvent):void {

         var errMsg:String = "xmlIOErrorHandler: " + event.toString();

         trace(errMsg + " file IO error ");

         xmlLoader1.removeEventListener(Event.COMPLETE,xmlLoadComplete1);

         xmlLoader1.removeEventListener(IOErrorEvent.IO_ERROR, xmlIOErrorHandler1);           

         }

       

      This is what my xml file looks like i'm trying to load.

      <data>

        <audit id="7011611">

          <comment>Opened</comment>

          <date>14261</date>

          <time>79367</time>

          <userid>20</userid>

          <employee>JEFF SANCHEZ</employee>

        </audit>

      </data>

       

      I'm not getting an error so i dont know, maybe it is loading right but i'm just not reading it right.  I'm trying to display it in an advanceddatagrid. Setting the dataprovider to {Data.audit}. Is that right or should i be setting it to something else?  Any help would be appreciated.

       

      Thanks,


      Gary

        • 1. Re: Loading XML files into flex application
          David_F57 Level 5

          hi,

           

          First with flex you can not programatically open a file outside the sandbox, for instance in a local testing if your project is in a folder called

           

          c:\myproject you can then request the xml  as "myxml.xml" if the xml was in

           

          c:\myproject\assets then the request path would be "assets/myxml.xml".

           

          if you were on a server and the file was either in the swf folder or below the swf path you could still use a relative reference. Otherwise you need the full url

           

          "http://mydomain.net/assets/myxml.xml".

           

          If you want the user to open an xml file then you can get it from the local harddrive using filereference which can open an OS based fileopen dialog box.

           

           

          David.

          • 2. Re: Loading XML files into flex application
            sensomage5 Level 1

            Is what i'm asking possible if i create an air application? or not.  Thanks for the insight!

            • 3. Re: Loading XML files into flex application
              David_F57 Level 5

              hi,

               

              Air apps are different you can get access to the local file system programmatically. There are several examples on the web about the File class which is what you need to read up on.

               

               

              David.

              • 4. Re: Loading XML files into flex application
                FlashGuruWiz
                function XMLload(onComplete) {
                   
                var request:URLRequest = new URLRequest("");
                    request
                .contentType = "text/xml";
                    request
                .data = "";
                    request
                .method = URLRequestMethod.POST;
                   
                var loader:URLLoader = new URLLoader();
                    loader
                .addEventListener(Event.COMPLETE ,function(e) { xmlParser(e); onComplete(e); } );
                   
                try
                   
                {
                        mainData
                .splice(0,mainData.length);
                        loader
                .load(request);
                       
                return true;
                   
                }
                   
                catch (e){
                       
                return false;
                   
                }
                }


                function xmlParser(e) {
                   
                var xml:XML = new XML(URLLoader(e.target).data);
                   
                }
                }