views:

93

answers:

2

In the following XML:

<myxml>
    <photos>
        <photo url="1.jpg"/>
        <photo url="2.jpg"/>
        <photo url="3.jpg"/>
        <photo url="4.jpg"/>
     </photos>
</myxml>

How do I use e4x to extract the urls and push them in an array with the least possible code?

+1  A: 

Hello, try that:

var picXML:XML;
var arrayPic:Array = new Array();
var stream:FileStream = new FileStream();
 stream.open("file.xml", FileMode.READ);
picXML = XML(stream.readUTFBytes(stream.bytesAvailable));
stream.close();

for(var i:int = 0; i < calendarsXML.photos.photo.length();i++)
    {
             arrayPic.push(myxml.photos.photo[i].@url);
    }
Mac Fly
+1  A: 

It's one line of code:

var myArray:Array = Array(myXML..@url);
trace(myArray); //outputs: 1.jpg2.jpg3.jpg4.jpg

Edit: Actually the above line produces an array with one long string of all the urls. in order to produce a useful array you would want to do this:

var myArray:Array = new Array();
for each(var item:XML in myXML..photo){
  myArray.push(item.@url);
}
trace(myArray); //outputs: 1.jpg,2.jpg,3.jpg,4.jpg
invertedSpear
That won't (I'm going to assume you wanted to use `Array.prototype.slice.call`) work as it expects `length` to be a number, not a function.
Eli Grey
??? What are you talking about? Your comment has nothing to do with my code example?
invertedSpear