A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: XML length & for-each

  1. #1
    Member
    Join Date
    Aug 2008
    Posts
    85

    XML length & for-each

    I have 10 dynamic text fields inside a movieclip, which are populated via an XML file. However, the xml file sometimes has less than 10 items. It never has more than 10. This results in the following error, which is normal. I need the code to count the amount of items in the XML file and then create a for-each loop. I can visualize it, but I don't know how to write it.


    Basically what I need the code below transformed in to is this... (notice the #'s)

    code:
    (count xml items)

    for each item {
    pb_mc.pb_#.htmlText="<b>MyContent:</b> "+updates.m[#].text();
    }



    TypeError: Error #1010: A term is undefined and has no properties.
    at mycontent_fla::MainTimeline/showXML()
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at flash.net::URLLoader/onComplete()


    Code:
    <?xml version="1.0"?>
    <pb>
    	<m>Blah blah blah my content text goes here and shows up in the first dynamic text field.</m>
    	<m>Blah blah blah....this would be the second dynamic text field... and so on</m>
    </pb>
    code:
    xmlLoader.addEventListener(Event.COMPLETE, showXML);

    function getMyContent() {
    xmlLoader.load(new URLRequest("http://www.mysite.com/myDynamicXML.xml"));
    }

    function showXML(e:Event):void {
    var updates:XML=new XML(e.target.data);
    pb_mc.pb_1.htmlText="<b>MyContent:</b> "+updates.m[0].text();
    pb_mc.pb_2.htmlText="<b>MyContent:</b> "+updates.m[1].text();
    pb_mc.pb_3.htmlText="<b>MyContent:</b> "+updates.m[2].text();
    pb_mc.pb_4.htmlText="<b>MyContent:</b> "+updates.m[3].text();
    pb_mc.pb_5.htmlText="<b>MyContent:</b> "+updates.m[4].text();
    pb_mc.pb_6.htmlText="<b>MyContent:</b> "+updates.m[5].text();
    pb_mc.pb_7.htmlText="<b>MyContent:</b> "+updates.m[6].text();
    pb_mc.pb_8.htmlText="<b>MyContent:</b> "+updates.m[7].text();
    pb_mc.pb_9.htmlText="<b>MyContent:</b> "+updates.m[8].text();
    pb_mc.pb_10.htmlText="<b>MyContent:</b> "+updates.m[9].text();
    }

    getMyContent();
    setInterval(getMyContent, 60000);// 60 sec


  2. #2
    newb of many sorts Ralgoth's Avatar
    Join Date
    Apr 2002
    Posts
    466
    Here's how I would do it...

    PHP Code:
    private function showXML(e:Event):void
    {
        var 
    updates:XML = new XML(e.target.data);
        var list:
    XMLList = new XMLList(updates.m);
        
        for (var 
    i in list){
            
    trace(i+': '+list[i]);
            
    pb_mc['pb_'+(Number(i)+1)].htmlText '<b>MyContent: </b>'+list[i];
        }

    Last edited by Ralgoth; 09-24-2009 at 08:16 AM.
    Search first, asked questions later.

  3. #3
    Senior Member
    Join Date
    Jul 2001
    Location
    Planet Earth
    Posts
    298
    1) Create a loader object

    Code:
    
    
    ---
    Thinking outside of the box will get you fired if the "box" is strict budget.

  4. #4
    newb of many sorts Ralgoth's Avatar
    Join Date
    Apr 2002
    Posts
    466
    oh yeah, the loop above will run through the entire list, but if you need to know how many items there then just call list.length()
    Search first, asked questions later.

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