15 Responses to “PHP parser for OS X plist XML files”

  1. Ryan Kennedy says:

    Nice, this should help with dumps from Delicious Library as well. The XML export uses plist as well.

  2. Tony Landis says:

    Thanks, this will be useful for me

  3. [...] Johnstone has come up with a simple plist xml parser. The plist format is what iTunes (and some other application) uses for its library [...]

  4. [...] Johnstone has come up with a simple plist xml parser. The plist format is what iTunes (and some other application) uses for its library [...]

  5. I am unable to parse plist file in http://www.adiumxtras.com/index.php?a=xtras&xtra_id=1431 others such.

    The reported error is: ‘Not a valid plist. key is not a valid type’.

  6. Jim says:

    If you aren’t set on writing your own thing to do this particular task (parsing the iTunes xml file), and you’re okay using tools Apple provides, look up the man page for PlistBuddy(8). iTunes comes with its own copy of the utility:

    /Library/Receipts/iTunesX.pkg/Contents/Resources/PlistBuddy

    It’s also at /usr/libexec/PlistBuddy.

    The command:
    PlistBuddy -c print iTunes\ Music\ Library.xml

    …will print every entry in the xml (aka plist) file. There are also commands for adding, deleting, copying, importing, merging, and modifying entries to the same file. You could conceivably wrap php around calls to PlistBuddy (e.g., for a web-driven interface); stuff like that makes me cringe but if it works for you that’s all that matters. :)

    Don’t know if it outputs in a format that you can use but it looks easier to deal with than the original xml.

    Keep in mind though that the XML file is never used by iTunes unless the user explicitly tells iTunes to import from it. The XML file is just a way for other programs to get at iTunes’ data without needing to parse the actual library file (the format of which seems to change with every new release of iTunes).

  7. Tom Shaw says:

    Nice but I can’t get it to read ~/Library/Preferences/com.apple.Safari.plist or ~/Library/Preferences/org.mozilla.firefox.plist Any ideas?

  8. ST says:

    Tom those plist files are in a binary format. This app (seems… I haven’t actually used it) to work on XML formatted plist files only.

    If you want to convert a binary plist into xml do this from terminal (using the correct path for your case):
    plutil -convert xml1 /Users/tomshaw/Desktop/Bookmarks.plist

    Don’t know if the converted file will work with this script but you can give it a shot.

  9. ST says:

    Just to clarify (using your example) you would do the above as follows (this assumes you have made a COPY of the plist file and put it on your desktop. Don’t do this to the original plist file as it will be made unreadable to Safari (though you can easily convert it back to the correct binary format:

    plutil -convert xml1 ~/Desktop/com.apple.Safari.plist

  10. AndreyP says:

    This is a bug in parse_array function. I think your problem, Gurpartap Singh, should be resolved by moving line 99 and 100 in original code to line 98 and 99 accordingly (i mean above “do”). Thx.

  11. drunknbass says:

    this doesnt seem to follow arrays at all. if i have a dict->array->10dicts it fails but if i have dict->array->1dict it works.

  12. Rodney Rehm says:

    PLists come either in XML or binary format. The parser written by Jeremy only works with the XML based PLists. Besides that, i could only find functionality for reading XML PLists.

    In my projects I needed some more power, especially when it came to creating PLists. So i wrote a little tool called CFPropertyList, which in fact was just a logic wrapper around PHP’s DOM. To simplify things in daily business, i also added functionality for guessing the PList structure from native PHP data structures. A friend of mine ported Apple’s CFBinaryPList to PHP, adding the result to CFPropertyList. So now we’ve got a fairly simple API to use Apple’s PropertyLists (XML and binary) in PHP. Reading, writing, creating, manipulating – everything you need.

    The tool was just launched this morning. Check it out at http://code.google.com/p/cfpropertylist/

  13. “The PHP implementation of Apple’s PropertyList can handle XML PropertyLists as well as binary PropertyLists. It offers functionality to easily convert data between worlds, e.g. recalculating timestamps from unix epoch to apple epoch and vice versa. A feature to automagically create (guess) the plist structure from a normal PHP data structure will help you dump your data to plist in no time.”

  14. Jonty says:

    Thanks for this, you saved me an hour of dullness! Just forked and pushed back a small changeset to allow parsing of strings as well as files, hope it’s useful to someone else.

  15. There is some plistParser with Adium plist files, i want to create site-smiles sysem based on adium smile-sets
    Try to parse plist from this set(http://www.adiumxtras.com/index.php?a=xtras&xtra_id=4207), it will crash with error:

    Fatal error: Uncaught exception ‘Exception’ with message ‘Not a valid plist. key is not a valid type’

Leave a Comment

Spam Protection by WP-SpamFree