Over a million developers have joined DZone.

Parsing Xml With Actionscript

·
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  node had node text
// Album Content
myData.album.__content__


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


#include "xml_deserializer.as"

/* Sample XML file

  
    
    
  

*/

// 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);
Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}