Friday, October 23, 2009

Read a property list XML file in Java

Plist for short, a property list XML file is used by the Mac Operating System to store information about a whole host of applications for Mac. It's even used by iTunes on Windows as a secondary interface for information about all the tracks and playlists in iTunes. I have been searching forever on how to read these files without having to design my own Java classes as Plist files are simply pointless. My research report was on XML and Plist xml files simply destroy the XML ideal in my opinion. Regardless, someone at Cupertino thought they were a good idea. I finally figured out how to read them using Apache Commons java packages.

I jotted this down in my notes for iTunes DSM but you basically need to download these packages:
  1. Apache Commons Configuration - Plist XML files
  3. Provides Exceptions for Apache
  5. Apache Logging
  7. Apache Predicate
  9. Apache Base64
The notes are just for me to know exactly what classes are required by the URL's listed there will take you to the web pages to download the packages. I usually just download the Jar files as it is easier later to simply replace one file should an update be needed. Once these packages are imported I use this code to get the location iTunes is storing music in.

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.plist.XMLPropertyListConfiguration;

public class ITunesXML {

private static String SPACE_CHAR = "%20";
private static String LOCATION_PREFIX = "file://localhost";

private XMLPropertyListConfiguration config;

public ITunesXML(String location){
config = new XMLPropertyListConfiguration(location);
catch(ConfigurationException e){
System.out.println("Exception ITunesXML Constructor");


public String getITunesDirectory(){
String loc = config.getString("Music Folder");
assert loc != null;
assert !loc.equals("");
loc = loc.replaceAll(SPACE_CHAR, " ");
loc = loc.replaceAll(LOCATION_PREFIX, "");
File file = new File(loc);
assert file.exists();
catch(Exception e){
System.out.println("Error in getITunesDirectory");
return loc;

public static void main(String[] args){
ITunesXML xml = new ITunesXML("C:\\location\\to\\iTunes Music Library.xml");



So here's a simple example on how to read the Mac OSX property list xml files in Java using Apache Commons packages! Now on to code the rest of the bug fixes.


No comments:

Post a Comment