# XML for each loop

**URL:** <https://forum.kirupa.com/t/xml-for-each-loop/263182>\
**Category:** flash\
**Created:** [June 13, 2008, 4:09am UTC](https://forum.kirupa.com/t/xml-for-each-loop/263182 "2008-06-13T04:09:37Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![craig\_clayton](https://avatars.discourse-cdn.com/v4/letter/c/ee59a6/32.png) [@craig\_clayton](https://forum.kirupa.com/u/craig_clayton)\
**Post date:** [June 13, 2008, 4:09am UTC](https://forum.kirupa.com/t/xml-for-each-loop/263182/1 "2008-06-13T04:09:37Z")

</div>

I have been at this for awhile now and I am struggling a little bit. I have figured out how to do a for each loop and get text to display in a text field inside a movieclip but I want to do the same thing with an image and seem to be having some trouble. My last question is I have added an event listener inside the loop and can trace it out but how can I target each one of those names since its created after the loop.

```auto

var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();

xmlLoader.addEventListener(Event.COMPLETE, LoadXML);

xmlLoader.load(new URLRequest("xmlThumbs.xml"));

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

function ParseNodes(myInput:XML):void {
    //trace(myInput.Photo.title);
    
    var myList:XMLList = myInput.Photo.title;
    var newList:XMLList = myInput.Photo.image;
    var thumbNail:circle;
    var i:uint = 0;
    
    for each (var myElement:XML in myList) {
        thumbNail = new circle();
        thumbNail.tf_title.text = myElement;
        thumbNail.addEventListener(MouseEvent.CLICK, thumbClick);
        thumbNail.name = "thumb" + i;
        thumbNail.x = 20;
        thumbNail.y = 20 + i * 100; 
        addChild(thumbNail);
        i++
    }
}

function thumbClick(evt:MouseEvent):void {
    trace(evt.target.name);
}

```

```auto

<Books>
    
    <Photo>
        <image>thumbs/thumb1.jpg</image>
        <title>Photo 1</title>
        <intro>Photo 1 Info</intro>    
    </Photo>
    
    <Photo>
        <image>thumbs/thumb2.jpg</image>
        <title>Photo 2</title>
        <intro>Photo 2 Info</intro>    
    </Photo>
    
    <Photo>
        <image>thumbs/thumb3.jpg</image>
        <title>Photo 3</title>
        <intro>Photo 3 Info</intro>    
    </Photo>
    
    <Photo>
        <image>thumbs/thumb4.jpg</image>
        <title>Photo 4</title>
        <intro>Photo 4 Info</intro>    
    </Photo>
</Books>

```

Thank you for anyone that can help me I have really be struggling with this and for some reason cant get past it.
