0

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.

2 Answers 2

1

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

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.
0

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); 
} 

2 Comments

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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.