Yeti
Yeti

Reputation: 5818

Converting xml attribute to array

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?

Upvotes: 1

Views: 461

Answers (2)

invertedSpear
invertedSpear

Reputation: 11054

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

Upvotes: 2

Mac Fly
Mac Fly

Reputation: 397

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

Upvotes: 1

Related Questions