views:

15

answers:

2

My Flash Code

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

myLoader.addEventListener(Event.COMPLETE, LoadXML);

myLoader.load(new URLRequest("mydata.xml"));

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

  trace(xmlData);
}

My XML Data (mydata.xml)

<xml>
  <items>
     <item Name="Test" ID="1" />
     <item Name="Home" ID="2" />
     <item Name="Car" ID="3" />
     <item Name="Balloon" ID="4" />
     <item Name="Harry" ID="5" />
     <item Name="Lion" ID="6" />
  </items>
</xml>

How can I get each item in the xml file to be able to then use it in my flash file.

I have tried several things but none seem to work. I am using ActionScript 3.0.

+1  A: 

Using E4X you can loop over each item in your XML

for each (var item:XML in xmlData.items.item) {
 trace(item.@Name, item.@ID);
}
Patrick
I am not getting an error using this but I don't get any trace items out. What might I be doing wrong. I copied what you have.
Nathan Stanford
A: 

I changed it to this xmlData..item and it worked. Does anyone know why?

for each (var item:XML in xmlData..item) { 
 trace(item.@Name, item.@ID); 
} 
Nathan Stanford
@Nathan Stanford, is your xml nodes really as in your example ? if not it will not trace anything, But if you do xmlData..item, it will look anywhere in your XML to find 'item' node, not only those from items.item. My version is more strict and respect only your example.
Patrick
It is that was why I was wondering what I might have been doing wrong. I figured your answer was right. Your answer makes sense.
Nathan Stanford