XML + YAML = iTunes XML

Definitions:

XML: eXtensible Markup Language

XML is the lingua franca of data transfer used in almost every situation that a computer geek can think of. It was designed to be both a human and machine readable format and does the job quite nicely.

An example of XML is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<root>
	<element attribute="true">
		<subelement />
		<subelement />
		<subelement />
		<subelement />
	</element>
</root>

YAML: YAML A‘int Markup Language1

Taken from the YAML 1.2 specification, it purports to be the following:

  1. YAML is easily readable by humans.
  2. YAML matches the native data structures of agile languages.
  3. YAML data is portable between programming languages.
  4. YAML has a consistent model to support generic tools.
  5. YAML supports one-pass processing.
  6. YAML is expressive and extensible.
  7. YAML is easy to implement and use.

and once again fulfills the stated goals quite nicely, here is an example taken from the afore mentioned specification:

invoice: 34843
date   : 2001-01-23
bill-to: &id001
    given  : Chris
    family : Dumars
    address:
        lines: |
            458 Walkman Dr.
            Suite #292
        city    : Royal Oak
        state   : MI
        postal  : 48046
ship-to: *id001
product:
    - sku         : BL394D
      quantity    : 4
      description : Basketball
      price       : 450.00
    - sku         : BL4438H
      quantity    : 1
      description : Super Hoop
      price       : 2392.00
tax  : 251.42
total: 4443.52
comments:
    Late afternoon is best.
    Backup contact is Nancy
    Billsmer @ 338-4338.

Note: The above weighs in at just over 600 characters

The two differences that we will be looking at are the following:

  1. Elements are named but not closed
  2. Indentation is used to separate elements

As you can see from the above example, it is human readable and quite concise – no closing of elements, no multitudes of sub-elements, if we were to re-write this as XML, it would look something along the following lines:

<?xml version="1.0" encoding="UTF-8"?>
<invoice id="34843" date="2001-01-23" tax="251.42" total="4443.52">
	<bill-to id="1d001">
		<name first="Chris" family="Dumars">
		<address>
			<line>458 Walkman Dr.</line>
			<line>Suite #292</line>
			<city>Royal Oak</city>
			<state>MI</state>
			<postal>48046</postal>
		</address>
	</bill-to>
	<ship-to ref="id001" />
	<product sku="BL394D" quantity="4" price="450.00">Basketball</product>
	<product sku="BL4438H" quantity="1" price="2392.00">Super Hoop</product>
	<comments>Late afternoon is best.
    Backup contact is Nancy
    Billsmer @ 338-4338.</comments>
</invoice>

Note: The XML also weighs in at slightly over 600 characters

So there isn’t much difference between the two formats, until you come to parsing them. But this is not what we are looking at.

How iTunes fits into this…

I am creating an example plugin for solrpanl which reads your itunes xml library file and adds it to the search index. This way you can set up a faceted search easily with data that you probably already have. Of course for those that are not iTunes users, other plugins would be more suitable.

Having dealt with XML/XSLT for many, many years I have never seen XML quite like the iTunes XML file2 – here is an example ‘snippet’ for you 3

    <key>542</key>
    <dict>
      <key>Track ID</key><integer>542</integer>
      <key>Name</key><string>The Long Way Around</string>
      <key>Artist</key><string>Dixie Chicks</string>
      <key>Album Artist</key><string>Dixie Chicks</string>
      <key>Album</key><string>Taking the Long Way</string>
      <key>Genre</key><string>Country</string>
      <key>Kind</key><string>Protected AAC audio file</string>
      <key>Size</key><integer>4591788</integer>
      <key>Total Time</key><integer>273436</integer>
      <key>Disc Number</key><integer>1</integer>
      <key>Disc Count</key><integer>1</integer>
      <key>Track Number</key><integer>1</integer>
      <key>Track Count</key><integer>14</integer>
      <key>Year</key><integer>2006</integer>
      <key>Date Modified</key><date>2006-12-18T20:52:59Z</date>
      <key>Date Added</key><date>2006-12-18T20:52:36Z</date>
      <key>Bit Rate</key><integer>128</integer>
      <key>Sample Rate</key><integer>44100</integer>
      <key>Release Date</key><date>2006-05-20T07:00:00Z</date>
      <key>Artwork Count</key><integer>1</integer>
      <key>Sort Name</key><string>Long Way Around</string>
      <key>Persistent ID</key><string>6E3C9C2B53372006</string>
      <key>Track Type</key><string>File</string>
      <key>Protected</key><true/>
      <key>Purchased</key><true/>
      <key>Location</key><string>file://localhost/Users/synapticloop/Music/iTunes/iTunes%20Music/Dixie%20Chicks/Taking%20the%20Long%20Way/01%20The%20Long%20Way%20Around.m4p</string>
      <key>File Folder Count</key><integer>-1</integer>
      <key>Library Folder Count</key><integer>-1</integer>
    </dict>

The above XML is basically a HashTable/Dictionary/XML-ised Property List – and here is where we get into trouble with parsing the XML file.

Notice that in the above you have two types of elements, the <key /> element and then the value element, where the value element is one of the following: date, real, integer, string, true, false. Instead of using elements such <album> or <genre>, Apple has chosen to use <key>album</key> and <key>genre</key>.

Whilst this allows a highly extensible XML file the semantic value of the key/value pairs is left up to the parsing application to decipher – which in this case is the solrpanl plugin. This isn’t all bad – it does make the SAX parser easy to code, however a reference to the ‘key’ element needs to be retained in order to make sense of the ‘value’ element – which just gets annoying. On the plus side for Apple and iTunes, this does mean that upgrading iTunes is very simple, – no parsing from one version of the XML file to another.

Footnotes:

  1. being the true geek that I am, I always love the recursive acronyms – the most famous probably being GNU GNUs Not Unix.
  2. I have to give it to Apple for one of the smallest DTDs I have ever seen. Have a look for yourself at the iTunes Playlist DTD – which is actually the Property List DTD
  3. Please excuse my lame taste in music!

Like my footnotes?
Want to add footnotes to your blog?
They can be added easily to your WordPress installation


Leave a Reply

You must be logged in to post a comment.