Accessing iTunes XML metadata with Python

Tutorial – iTunes XML

Article from Issue 264/2022

Read and manipulate data from your iTunes XML in order to share music metadata between applications and across devices.

The Extensible Markup Language (XML)[1] is a widely used markup language and text file format for storing and exchanging arbitrary data. A wide variety of applications, including the Apple iTunes application, use the XML format for exchanging or storing library data. In this article, I will show you how to use Python to read and manipulate data from an iTunes [2] XML library file.

iTunes Library File

iTunes divides its content into two separate categories: media and metadata. Media (music and video) are saved in separate files from metadata. Throughout the rest of this article, I will focus on music information – artist, song title, album title, track number, genre, release year – and refer to it as metadata. The iTunes metadata files have either a .itl or .xml extension (Figure 1).

Figure 1: The iTunes XML metadata file hierarchy.

Why Choose XML?

If you are a music lover like me, there may be times when you want to exchange music metadata between a variety of applications on a variety of digital devices. In my case, I have imported iTunes metadata into database applications to run SQL queries. Some applications use proprietary formats to get optimum performance and efficiency but limit access with proprietary products. The iTunes .itl format is an example. On the other hand, there are open formats that place more of an emphasis on wide availability and ease of development. XML is in the open format category.

Apple has historically used XML files to export library and playlist metadata to external applications. While the native format for the iTunes library is a binary file with a .itl extension, users can configure iTunes to automatically save an XML copy of the library via Advanced preferences or manually via the File | Export menu. If you prefer to export a subset of the library, the File | Export menu allows you to save playlists to an XML file.

Advantages of the XML format are that it is both human-readable and machine-readable. The benefit of having a human-readable file is being able to easily inspect and diagnose the data with a simple text editor. Machine readability makes it easy for applications to access the metadata via an application procedural interface (API). Many applications that we use every day – text editors, word processors, spreadsheet editors, presentation tools, and graphics editors – either use XML as the native format or to exchange data with other applications.

Python XML Module

The Python standard library has structured markup tools that include modules for processing XML. In this article, I will describe how metadata in iTunes XML files is organized and how to use the Python xml.etree.ElementTree [3] module to navigate and access that data. In order to extract metadata from an iTunes XML playlist or library file, it is useful to understand how the XML file is structured. Songs within a library or playlist file are organized as a list of tracks identified by XML tag <key>Tracks</key>. Metadata for each track consists of a dictionary of key-value pairs.

The XML language allows for data types to be defined in a Document Type Definition (DTD) that can be declared either inline inside an XML document or as an external reference. Apple traditionally uses property list files for their application configuration, thus the term "property list" (plist) in the declaration (Listing 1). Because metadata is structured as elements in an XML tree, the xml.etree.ElementTree module, which stores tree elements hierarchically, is an appropriate way to get to those elements. Elements are defined in the external public DTD link specified in the DOCTYPE declaration on the second line of the exported music file.

Listing 1

DTD in DOCTYPE Declaration

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "">

The data types defined in this DTD are: array, data, date, dict, real, integer, string, true, and false. XML elements are also declared in the DTD file. An element is a logical document component that begins with a start tag and ends with a matching end tag. Each metadata element contains a string that is either a key or a value in the music dictionary. Metadata for song tracks is organized as key-value pairs as illustrated in Listing 2, where a <key> tag with a text string is followed by a <string> or <integer> tag with a text string.

Listing 2

XML Track Metadata Example

<string>Justice's Grove</string>
<string>Stanley Clarke</string>
<string>East River Drive</string>
<key>Track Number</key>

In Table 1, you will see Python code snippets to show you how to use the listed methods and attributes to access metadata in XML elements.

Table 1

Accessing Metadata in XML Elements

Code Snippet



Sources and parses an external XML file or file object


Returns the root element for the tree


Returns an instance of the first specified sub-element


Returns a list containing all matching elements in document order


Specifies the element name as defined in the DTD


Specifies the characters between the start and end tag

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Musical Talent: Songbird 1.2 has Landed

    The new version automatically organizes music libraries and is fully integrative with iTunes.

  • Firefly Audio Streaming

    The Firefly Media Server makes streaming music and Internet radio onto your home network for iTunes or Banshee clients as easy as pie.

  • LibreOffice Music Database

    LibreOffice Calc and Base are all you need to create a simple database for organizing the songs in your music collection.

  • Banshee

    In Irish mythology, the banshee’s mourning call is heard when a member of the family is about to die. The Banshee tool on Linux makes noise too, but for a far happier purpose. This banshee helps you organize your musical collection.

  • Managing Music with Picard

    Getting that iTunes experience requires more than just Amarok or Rhythmbox. It also requires planning – especially if you went digital before the Linux desktop had audio players.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More