DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Parsing Xml With Actionscript

08.31.2005
| 24703 views |
  • submit to reddit
        The Xml Deserializer functions parse an XML file and give you a basic object of the nodes and attributes.  Using the discography xml fragment below as an example, you can do:

myData.album._title
myData.album.song[0]._href;

// this is if the <album> node had node text
// <album title="blah">Album Content</album>
myData.album.__content__

Pick it up piping hot at http://rubyurl.com/q05.

#include "xml_deserializer.as"

/* Sample XML file
<discography>
  <album title="Late For The Sky" year="1974">
    <song title="Late For The Sky" href="late_for_the_sky.mp3"/>
    <song title="Fountain Of Sorrow" href="fountain_of_sorrow.mp3"/>
  </album>
</discography>
*/

// xml callback function
function parseDiscography(success) {
  if(!success) return;
	
  XmlDeserializer.parse_nodes(myData, XmlDeserializer.xml_object.childNodes[0]);
	
  play();

  var albums = XmlDeserializer.get_items(myData.album);
  for(var a=0; a < albums.length; a++) {
    var album = albums[a];
    var songs = XmlDeserializer.get_items(album.song);
    for(var s=0; s < songs.length; s++) {
      var song = songs[s];
      if(song._href != undefined) {
        var song_obj = { 
          title: album._title + ' :: ' + song._title,
          href: myDiscoPath + song._href
        };

        // global array used by flash
        mySongs.push(song_obj);
      }
    }
  }
}

// object used by XmlDeserializer class
var myData = {};
// strip off disco.xml
var myDiscoPath = myDisco.substr(0, myDisco.length-9); 
//XmlDeserializer.debug = true;

// myDisco is url to discography xml file
XmlDeserializer.parse_xml(myDisco, parseDiscography);