A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Cant get XML data with AS3 [Please Help]

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    6

    Cant get XML data with AS3 [Please Help]

    I am trying to get some xml data in to my textfield, but i keep getting error. I need to keep the xml format, so please let me know what i should change in the AS3 code.

    My AS3:
    //Load XML
    var xmlLoader:URLLoader = new URLLoader();
    var xmlData:XML = new XML();
    xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
    xmlLoader.load(new URLRequest("myXml.xml"));

    function LoadXML(e:Event):void {
    xmlData = new XML(e.target.data);
    ParseVdo(xmlData);
    }

    function ParseVdo(vdoInput:XML):void {
    textField1.text = (vdoInput.gallery.items.@title[00]);
    textField2.text = (vdoInput.gallery.items.@title[01]);
    textField3.text = (vdoInput.gallery.items.@title[02]);

    }

    My XML:
    <?xml version="1.0" encoding="UTF-8"?>
    <gallery>

    <album id="vdo" lgPath="images/home/">
    <img src="vdo_01.f4v" title="test 01"/>
    <img src="vdo_01.f4v" title="test 02"/>
    <img src="vdo_01.f4v" title="test 03"/>
    </album>

    <album id="img" lgPath="images/home/">
    <img src="vdo_01.f4v" title="test 04"/>
    <img src="vdo_01.f4v" title="test 05"/>
    <img src="vdo_01.f4v" title="test 06"/>
    </album>

    </gallery>

  2. #2
    Member
    Join Date
    Dec 2009
    Posts
    84
    This should work for you. I personally am not a fan of passing an xml into a function via a variable when it is already accessible, but that is just a personal preference. Your problem appears to just be the path you used. Here is the code that worked for me:
    Actionscript Code:
    //Load XML
    var xmlLoader:URLLoader = new URLLoader();
    var xmlData:XML;
    xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
    xmlLoader.load(new URLRequest("myXml.xml"));

    function LoadXML(e:Event):void {
        xmlData=new XML(e.target.data);
        ParseVdo();
    }

    function ParseVdo():void {
        textField1.text=xmlData..album[0].img[0].@title;
        textField2.text=xmlData..album[0].img[1].@title;
        textField3.text=xmlData..album[0].img[2].@title;
    }

    but you could also change your path to (vdoInput..album[0].img[0].@title) and it should work fine.

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    6
    Thanks so much! your code work and save my day.

    You mention it might not be a good idea to pass an xml into a function via a variable. If you can post the code in a better version that would help.

    again thank you so much for your time.

  4. #4
    Member
    Join Date
    Dec 2009
    Posts
    84
    That is in the code I posted. Notice that my ParseVdo function does not have an input variable. Since you defined xmlData above outside of any functions, it is accessible from anywhere in your swf. Even if you were on frame 263, you can still access xmlData. You can still load it into ParseVdo as a variable like you did, I just see it as an extra variable to keep track of. It is completely a personal preference though.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center