XSPF.toJSPF() spec
XSPF.toJSPF() is a function for parsing XSPF in Javascript. The resulting Javascript object is easy to use and manipulate to create client-side playlist browsers and media players. The parsed result can also easily be serialized into JSON for easy server-client transport. This allows developers to use one player for handling both XML XSPF and JSON JSPF playlists provided by the server.
This file tests XSPF.toJSPF(), which takes as its argument and XML DOM object, and returns a Javascript object. It requires spec.js and the Prototype library to run, as well as the xspf fixtures directory.
Copyright 2007 J. Chris Anderson. Released under the LGPL 2.1 Retain this notice.
Extracted from the Grabb.it Online Music Player.
JSPF as JSON serialization
The parser outputs a javascript object structure identical to what can be obtained by eval'ing the following text, which makes the format well suited to JSON transport. To get an idea of the data structure returned by XSPF.toJSPF() you can copy the following code and paste it into Firebug. (You must put it in parenthesis or assign it to a variable to avoid syntax errors.)
{ title: "Alan Singley on Music For Dozens",
tracks: [
{
locations: ["http://local.grabb.it/tracks/bc216dfd4275.mp3"],
title: "Sitting Silent",
trackNum: 4,
annotation: "Sitting Silent by Alan Singley",
identifiers: ["http://grabb.it/tracks/bc216dfd4275"],
duration: null,
image: "http://mfdz.com/image/profile/760.jpg",
extensions: {"http://grabb.it": {
stars: 1,
artist_id: 8,
gid: "bc216dfd4275"}},
info: "http://grabb.it/tracks/bc216dfd4275",
album: "Less Paul, More Rock",
creator: "Alan Singley"
},{
locations: ["http://local.grabb.it/tracks/84180f5208ab.mp3"],
title: "It's OK When It's Just You & I",
trackNum: null,
annotation: "It's OK When It's Just You & I by Alan Singley",
identifiers: ["http://grabb.it/tracks/84180f5208ab"],
duration: null,
image: "http://mfdz.com/image/profile/760.jpg",
extensions: {"http://grabb.it": {
stars: 1,
artist_id: 8,
gid: "84180f5208ab"}},
info: "http://grabb.it/tracks/84180f5208ab",
album: null,
creator: "Alan Singley"
},{
locations: ["http://local.grabb.it/tracks/84887bce1749.mp3"],
title: "March 23",
trackNum: null,
identifiers: ["http://grabb.it/tracks/84887bce1749"],
annotation: "March 23 by Alan Singley",
duration: null,
image: "http://mfdz.com/image/profile/760.jpg",
extensions: {"http://grabb.it": {
stars: 0,
artist_id: 8,
gid: "84887bce1749"}},
album: "Secretly Awesome",
info: "http://grabb.it/tracks/84887bce1749",
creator: "Alan Singley"
}
],
location: "http://grabb.it/playlists/c9604a313857",
annotation: null,
extensions: {"http://grabb.it": {gid: "c9604a313857"}},
info: "http://mfdz.com/alan",
creator: null
}
Original XSPF
This is the sample XSPF from which the above Javascript object was parsed.
<?xml version="1.0" encoding="UTF-8"?>
<playlist xmlns="http://xspf.org/ns/0/" version="1">
<title>Alan Singley on Music For Dozens</title>
<creator/>
<info>http://mfdz.com/alan</info>
<annotation/>
<location>http://grabb.it/playlists/c9604a313857</location>
<extension application="http://grabb.it">
<gid>c9604a313857</gid>
</extension>
<trackList>
<track>
<creator>Alan Singley</creator>
<title>Sitting Silent</title>
<annotation>Sitting Silent - Alan Singley</annotation>
<album>Less Paul, More Rock</album>
<trackNum>4</trackNum>
<image>http://mfdz.com/image/profile/760.jpg</image>
<location>http://local.grabb.it/tracks/bc216dfd4275.mp3</location>
<identifier>http://grabb.it/tracks/bc216dfd4275</identifier>
<info>http://grabb.it/tracks/bc216dfd4275</info>
<extension application="http://grabb.it">
<artistid>8</artistid>
<gid>bc216dfd4275</gid>
</extension>
</track>
<track>
<creator>Alan Singley</creator>
<title>It's OK When It's Just You & I</title>
<annotation>It's OK When It's Just You & I - Alan Singley</annotation>
<image>http://mfdz.com/image/profile/760.jpg</image>
<location>http://local.grabb.it/tracks/84180f5208ab.mp3</location>
<identifier>http://grabb.it/tracks/84180f5208ab</identifier>
<info>http://grabb.it/tracks/84180f5208ab</info>
<extension application="http://grabb.it">
<artistid>8</artistid>
<gid>84180f5208ab</gid>
</extension>
</track>
<track>
<creator>Alan Singley</creator>
<title>March 23</title>
<annotation>March 23 - Alan Singley</annotation>
<album>Secretly Awesome</album>
<image>http://mfdz.com/image/profile/760.jpg</image>
<location>http://local.grabb.it/tracks/84887bce1749.mp3</location>
<identifier>http://grabb.it/tracks/84887bce1749</identifier>
<info>http://grabb.it/tracks/84887bce1749</info>
<extension application="http://grabb.it">
<artistid>8</artistid>
<gid>84887bce1749</gid>
</extension>
</track>
</trackList>
</playlist>