1 Reply Latest reply on Aug 4, 2010 1:32 AM by tjay99

    XML to display image from userDirectory

    tjay99 Level 1

      Hi All,

       

      I've been racking my brain for weeks now trying to work out how to do the following. I can do some of it in Flash and some of in Flex, but I can't do it all in Flex which is frustrating.

       

      In a nutshell, I want to create an Air App that will allow me to load an XML file from:

       

      File.userDirectory

       

      and then display images or SWF's based on the XML data:

       

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

       

      <?xml version="1.0" encoding="utf-8"?>
      <GALLERY>
      <IMAGE TITLE="shop">test_4.swf</IMAGE>
      </GALLERY>

       

      ===========

       

      In Flex 3 I can get the XML file loading from the userDirectory no problem, but I can't work out how to code it to display the images on the stage.

       

      In Flash I can get the XML file loading from the app's installation directory only, but I can display the images on the stage.

       

      Here are the two lots of code:

       

      FLASH (Note: I have a few movieclips holding the images)

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

       

      var myXML:XML;
      var myLoader:URLLoader = new URLLoader();
      myLoader.load(new URLRequest("example.xml"));
      myLoader.addEventListener(Event.COMPLETE, processXML);
      function processXML(e:Event):void {
      myXML = new XML(e.target.data);
      //trace(myXML.IMAGE);
      for (var i:int = 0; i<myXML.*.length(); i++) {
        trace("My image number is " + (i+1) + ", it's title is " + myXML.IMAGE[i].@TITLE + " and it's URL is " + myXML.IMAGE[i]);
        var url:URLRequest = new URLRequest(myXML.IMAGE[i]);
        var loader:Loader = new Loader();
        loader.load(url);
        switch (i) {
         case 0 :
          trace(0);
          mc_container_0.addChild(loader);
          break;
         case 1 :
          trace(1);
          mc_container_1.addChild(loader);
          break;
         case 2 :
          trace(2);
          mc_container_2.addChild(loader);
          break;
         default :
          //trace("Not 0, 1, or 2");
        }
      }
      }

       

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

       

      and:

       

      FLEX 3

       

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

       

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

      <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"

      width="768" height="1366" xmlns:comp="*" xmlns:fmtComp="fmtComps.*"

      creationComplete="readXMLContent();" paddingLeft="0" paddingRight="0" paddingTop="0" paddingBottom="0" backgroundColor="#ff00ff">

      <mx:Script>

      <![CDATA[

      import mx.controls.Alert;

      import mx.utils.ObjectProxy;

      import mx.rpc.events.ResultEvent;

      [Bindable]

       


      private var userManualObj:ObjectProxy;

      private function loadModelData(event:ResultEvent):void

      {

      userManualObj=event.result.UserManual;

      }

      private function readXMLContent():void

      {


      var reportDir:File = File.userDirectory.resolvePath("myassets");

      var xmlFile:File;

      var fileStream:FileStream = new FileStream();

      xmlFile = reportDir.resolvePath("example.xml");

      if(xmlFile.exists && xmlFile.size > 0)// Checking if the UserGuide.xml file already exists and the file is not empty

      {

      var stream:FileStream = new FileStream();

      var xml:XML = new XML();

      stream.open(xmlFile, FileMode.READ);

      xml = XML(stream.readUTFBytes(stream.bytesAvailable));

      var strXml:String = xml.toXMLString();

      Alert.show(strXml);

      trace(strXml);

      stream.close();

      }

      }

       

       

      ]]>

      </mx:Script>

       

      <mx:SWFLoader height="93" width="708" id="mc_container_0" />


      <mx:SWFLoader height="393" width="708" id="mc_container_1" />


      <mx:SWFLoader height="293" width="708" id="mc_container_2" />

       

      </mx:WindowedApplication>

       

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

       

      Somehow I need to get the XML data for the file location into each of the SWF loaders in Flex, but I simply don't know how to code it? It's probably very easy for an expert out there.

       

      Note: I only want to end up with an Air App which will be running locally on my machine.

       

      The idea is, when the XML file changes with new image data, the Air App will display that new image. The image will be added to the "myassets" folder too.

       

      If anyone out can point me in the right direction, it would certainly be appreciated.

       

      Many thanks.

       

      Tim