I am trying to parse the XML file that I retreive from the following URL using an Http Request in Android:
http://www.musicbrainz.org/ws/2/recording/?query=kick%20AND%20artist:inxs
What would be the best way to parse it? I need to find the artist name, the album title and release id. I have been trying to use the method specified here:
http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html
But it it turning out to be really ineffecient. Any suggestion for a library or an easier method I can use to retrieve the required information?
My code so far is as follows:
public List parse(InputStream in) throws XmlPullParserException, IOException {
try {
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in, null);
parser.nextTag();
return readFeed(parser);
} finally {
in.close();
}
}
private List readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
List entries = new ArrayList();
parser.require(XmlPullParser.START_TAG, ns, "metadata");
Log.i("XMLParser", "metadata");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
// Starts by looking for the entry tag
if (name.equals("recording-list")) {
Log.i("XMLParser", "recording-list");
entries.add(readRecordingList(parser));
} else {
skip(parser);
}
}
return entries;
}
private Recording readRecordingList(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, "recording-list");
Recording recording=new Recording(null,null,null,null);
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("recording")) {
Log.i("XMLParser", "recording");
recording = readRecording(parser);
} else {
skip(parser);
}
}
return recording;
}
private Recording readRecording(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, "recording");
Recording recording=new Recording(null,null,null,null);
String albumTitle=null;
String artistName=null;
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
Log.i("XMLParserNodeName", name);
if (name.equals("title")) {
Log.i("XMLParser", "title");
albumTitle=readTitle(parser);
Log.i("XMLParser", albumTitle);
//recording = readRecording(parser);
}else if(name.equals("name")){
Log.i("XMLParser", "name");
name=readArtistName(parser);
Log.i("XMLParser", name);
}else {
skip(parser);
}
}
return recording;
}
private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
if (parser.getEventType() != XmlPullParser.START_TAG) {
throw new IllegalStateException();
}
int depth = 1;
while (depth != 0) {
switch (parser.next()) {
case XmlPullParser.END_TAG:
depth--;
break;
case XmlPullParser.START_TAG:
depth++;
break;
}
}
}
private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
return result;
}
private String readTitle(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "title");
String title = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "title");
return title;
}
private String readArtistName(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "name");
String name = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "name");
return name;
}
public static class Recording {
public final String recordingTitle;
public final String releaseTitle;
public final String releaseID;
public final String artistName;
public Recording(String recordingTitle, String releaseTitle,
String releaseID, String artistName) {
this.recordingTitle = recordingTitle;
this.releaseTitle = releaseTitle;
this.releaseID = releaseID;
this.artistName = artistName;
}
}
}
Basically this is an adaptation from the XML Pull Parser Tutorial in Android but I want to avoid writing so much code each node and the xml file has a lot of them. I think I would prefer XML Pull Parser over DOM so that I don't consume a lot of memory.