A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Can you create a "for loop" to populate XML with more nodes?

  1. #1
    Senior Member
    Join Date
    Nov 2004
    Location
    nj
    Posts
    176

    Can you create a "for loop" to populate XML with more nodes?

    I have a flash gallery that works and I have it calling an XML file to populate the images. It's real basic stuff. The issue is that I have 251 images in my folder and I don't feel like typing all those nodes. Is there a way to create a for loop in my xml file so I can adjust the i <= "number of images" info?

    Here's the code so far with just a few images -

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>

    <images>

    <pic>
    <image>images/img1.jpg</image>
    </pic>
    <pic>
    <image>images/img2.jpg</image>
    </pic>
    <pic>
    <image>images/img3.jpg</image>
    </pic>
    <pic>
    <image>images/img4.jpg</image>
    </pic>
    </images>

  2. #2
    Senior Member
    Join Date
    Aug 2006
    Posts
    322

    Yes, you can

    If the path and the file extension are static. Say for example:
    path = images/
    ext=.jpg

    fileName=img0,img1,img2...........

    Then you can play with for loop.

    the xml file will just a few line:

    PHP Code:
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <root>
    <images nNum="251" nExt=".jpg">
    <image>images/img</image>
    </images>
    </root>
    Actionscript Code:
    var xml:XML = new XML();
    xml.onLoad = function(success:Boolean) {
        if (success) {
            nNum=(xml.firstChild.nextSibling.firstChild.nextSibling.attributes.nNum);
            nExt=(xml.firstChild.nextSibling.firstChild.nextSibling.attributes.nExt);
            nPath=(xml.firstChild.nextSibling.firstChild.nextSibling.firstChild.nextSibling.firstChild.nodeValue);
            for(var i=0;i<nNum;i++){
            this["imgPath"+i]=(nPath+i+nExt);
            trace(this["imgPath"+i]);
            }
        }
    };
    xml.load("http://www.domainName/xmlFileName.xml",_root);

    Regards

    marlopax
    Last edited by marlopax; 10-16-2010 at 05:39 PM.

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