Search code examples
actionscript-3actionscriptflash-cs4flash-cs5

Loading XML file using as3


public class loadXML extends Sprite
{
    public var xmlFileName:String;
    public var urlL:URLLoader = new URLLoader();
    public var urlR:URLRequest;
    public var xml:XML = new XML();

    public function loadXML(xmlS:String):void
    {
        xmlFileName = xmlS;
        urlR =  new URLRequest(xmlFileName);            
        urlL.addEventListener(ProgressEvent.PROGRESS, onProgressAction);
        urlL.addEventListener(Event.COMPLETE, onLoadedAction);
        urlL.addEventListener(IOErrorEvent.IO_ERROR, errorAction);
        urlL.load(urlR);            
    }
    public function onLoadedAction(e:Event):void
    {
        xml = XML(e.target.data);
        //trace(xml);
    }       
    public function onProgressAction(e:ProgressEvent):void
    {
        //trace("loading xml");
    }
    public function errorAction(e:IOError):void
    {
        trace(e.toString());
    }
}
}

//main class

package
{
import com.loadXML;
import flash.display.Sprite;

public class xmlFileView extends Sprite
{
    public var xmlData:loadXML;
    public function xmlFileView():void
    {
        init();
    }
    private function init():void
    {
        xmlData = new loadXML("list.xml");
        var xmlF:XML = XML(xmlData);
        //trace(xmlF.video[0].path);

    }
}

}

Here I instantiate the loadXML class. But I can't able to access the xml, y? and how can I achieve that?

here is my XML file.

<?xml version="1.0" encoding="iso-8859-1"?>
<videos>
    <video>
        <path>video1.flv</path>
    </video>
    <video>
        <path>video2.flv</path>
    </video>
    <video>
        <path>video3.flv</path>
    </video>
    <video>
        <path>video4.flv</path>
    </video>
</videos>

Solution

  • the xml you have there is looking not like xml :) here how it should look like reffering the trace(xmlF.video.path[0]);

    <?xml version="1.0" encoding="utf-8" ?>
    <data>
        <video>
            <path>video1.flv</path>
            <path>video2.flv</path>
            <path>video3.flv</path>
            <path>video4.flv</path>
        </video>
    </data>
    

    UPDATE

    you can not do like this:

    xmlData = new loadXML("list.xml");
    var xmlF:XML = XML(xmlData);
    

    because loading something takes time, and script is not waiting until the loading has been done.

    If you want to achive this you need to do it following way: in the loadXML class edit function:

    public function onLoadedAction(e:Event):void
    {
        xml = XML(e.target.data);
        // dispatch event when the XML has been loaded and xml value is defined.
        dispatchEvent ( new Event ( Event.COMPLETE ) )
    }
    

    in the xmlFileView class edit:

    private function init():void
    {
        xmlData = new loadXML("list.xml");
        // listen for loading completed
        xmlData.addEventListener ( Event.COMPLETE, handleXMLLoaded );
    }
    
    private function handleXMLLoaded (e:Event):void
    {
       var xmlF:XML = xmlData.xml
       trace(xmlF.video.path[0]); 
    }