You might want to take a look at the XML pull parser tutorial on the Android Developers site. Here's a partial outline of a solution adapted from that tutorial.
First create a method that goes through the document and looks for the top-level tags you're interested in:
private void readDocument(XmlPullParser parser) throws XmlPullParserException, IOException {
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("name")) {
readName(parser);
} else if (name.equals("currency"))
readCurrency(parser);
else {
skip(parser);
}
}
}
Then handle each tag with its own method, repeating the process as necessary:
private void readCurrency(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, "currency");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("euro")) {
euro = readEuro(parser);
} else if (name.equals("dollar")) {
dollar = readDollar(parser);
} else {
skip(parser);
}
}
}
// Read in the content of the Euro element.
private String readEuro(XmlPullParser parser) {
return readText(parser);
}
I've omitted the skip()
and readText()
methods, which are detailed in the tutorial, and you'll obviously also want to add code to store the data you read in somehow. But hopefully that gives you an idea of how you can structure your parser to deal with the nested elements.