iTunes Library Analysis
iTunes is a piece of software that organises media files. It uses a text file format called XML which is used by many systems to store data in a standard format. This file can be read like any file and analysed.
Your task is to read an iTunes library file, and from it produce a file that logs which genres are present in the library and how many tracks belong to each genre. For example the file contains details of an album called White Rabbit which is of the Jazz genre. All the tracks on that album should be included in the count for the Jazz genre, along with the tracks for any other Jazz albums. You cannot know in advance how many genres there are, or how many albums, or how many tracks. Your program must discover this as it runs and analyses the contents of the file.
Write this analysis to a new file, called analysis.txt. Along with the total number of tracks found.
The XML line that contains the genre information of a track looks like this:
<key>Genre</key><string>Pop</string>
To tackle this problem think about the following:
- How do you find the lines containing the genre?
- What do you do once you find the line containing the genre?
- What data structures can you use to store the data that is found?
- Once you have the results, what do you need to do to write the results to the output file?
Use the answers to the above to produce an algorithm, then code a solution in Java. It will use almost all of the skills you have learned so far, including selection, iteration, string manipulation, arrays and file reading and writing.
Useful string functions you might use include:
- indexOf (to find the position of a string within another string).
- substring(to extract part of a string into another string)
- contains(to see if one string is inside another)
//skeleton code for iTunes analysis application
import java.io.*;
import java.util.*;
public class AnalysisApp{
public static void main(String[] args) {
String inputFilename = "iTunes Music Library.xml";
String outputFilename = "analysis.txt"; // The program will create this file
try {
PrintWriter outFile = new PrintWriter(new FileWriter(outputFilename),true);
File inFile = new File(inputFilename);
Scanner fileScanner= new Scanner(inFile);
while (fileScanner.hasNextLine()) {
//your code here
}
outFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Your program obviously needs a source file from iTunes. You can usually find it in the Music\iTunes folder in Windows. Ask your teacher or a friend for a copy of their iTunes Music Library.xml file if you don’t use iTunes.
An example of output from a working program is below.
Analysis of iTunes Music Library Small.txt shows it has 13 genres. Details follow below.
Electronic/Chill-Out:3
Trip Hop:19
Rock:62
Alternative:86
R&B:24
Asian Urban:14
Classic Rock:38
Indie Rock:12
Pop:43
Soul:63
Jazz:14
Alt-Folk:12
Electronica:20
There are 410 tracks in total.
(The above file has been renamed with a .txt extension as WordPress doesn’t like uploading of XML files)