Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Use Javascript to parse XML

I'm in need of help trying to parse an XML file to populate a JPlayer playlist. Not sure what is the best approach for this.

JPlayer uses JSON format for it's playlist items. The playlist is an XML file. I use the xml2json jQuery plugin to convert the XML to JSON.

Sofar I created a basic prototype object for a playlist track.

var $track = {
    mp3Location : "path/to/mp3",
    oggLocation : "path/to/ogg",
    creator     : "name",
    album   : "albumname",
    title       : "track title",
    annotation  : "description",
    poster  : "path/to/img" 
    }
//create prototype for track object 
function Track (title) {
    this.title = title;
    };  
Track.prototype = $track;

After this I want to use the plugin to read the contents of the XML and convert to JSON. I'm looking for a way to create new prototypes based on the $track prototype. The fields in the XML file correspond with the $track prototype keys.

$.get('xml/GezondheidList.xml', function (xml) {
    var xml = $.xml2json(xml);
    //use xml2json to convert each <track> into track[n] json object
    });

After this the contents have to be read into the JPlayer playlist.

var myPlaylist = new jPlayerPlaylist({
    jPlayer : "#jquery_jplayer_1",
    cssSelectorAncestor: "jp_container_1"
    }, [], {});
myPlaylist.setPlaylist([
//load json objects from $tracks
$track.map(function (){
    return $(this).val();
    })
]); 
});