It’s easy enough to read in an xml file from a url and parse it using the SAX parser.
There’s plenty of examples available.
But reading an external xml file from the sdcard required a little extra reading, so I decided to put this up in the hopes that it helps someone out.
XmlPullParser works great when your xml is simple enough.
In the example below, I’ll take you through the setup and then through how to actually parse a piece of xml.
public void parseXML() throws XmlPullParserException, IOException { XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); factory.setNamespaceAware(true); XmlPullParser xpp = factory.newPullParser(); // get a reference to the file. File file = new File(Environment.getExternalStorageDirectory() + "/digital_lounge/xmltest2.xml"); // create an input stream to be read by the stream reader. FileInputStream fis = new FileInputStream(file); // set the input for the parser using an InputStreamReader xpp.setInput(new InputStreamReader(fis)); int eventType = xpp.getEventType(); ...}
First we create a parser from the XMLPullParserFactory.
Then we get a reference to the file we want to deal with
// get a reference to the file. File file = new File(Environment.getExternalStorageDirectory() + "/digital_lounge/xmltest2.xml"); // create an input stream to be read by the stream reader. FileInputStream fis = new FileInputStream(file);
Then we set it as an input for the parser. The trick here is that you have to feed it to the parser as an InputStreamReader. So it works a little like this. file reference > FileInputStream >pullParser(InputStreamREader)
And that’s it! now you’re ready to parse your xml.
The XmlPullParser iterates through every node in your xml.
As it loops through it will fire off events that you can access via the getEventType() method.
Here are the event types that matter.
START_TAG
An XML start tag was read.
TEXT
Text content was read; the text content can be retrieved using the getText() method. (when in validating mode next() will not report ignorable whitespace, use nextToken() instead)
END_TAG
An end tag was read
END_DOCUMENT
No more events are available
For more info see the docs here.
Now for the parsing.
What i’m doing looking for the START_TAG event type, getting the name of the node that just starting and basing my logic on that. I compare the nodes i’m interested in and begin populating my objects based on that. I think it works.
Here’s what that looks like:
String nodeName = xpp.getName(); if (nodeName.contentEquals(APPLICATIONS)) { appList = new ArrayList(); currentSection = 0; } if (nodeName.contentEquals(GAMES)) { gameList = new ArrayList (); currentSection = 1; } if (nodeName.contentEquals(MUSIC)) { musicList = new ArrayList (); currentSection = 2; } if (nodeName.contentEquals(VIDEOS)) { videoList = new ArrayList
The constants correspond to the nodes in my xml.
private static final String APPLICATIONS = "applications"; private static final String GAMES = "games"; private static final String VIDEOS = "videos"; private static final String MUSIC = "music"; private static final String ATTRACT_VIDEOS = "attract_loops"; // repeating nodes private static final String APP = "app"; private static final String DESCRIPTION = "description"; private static final String DEVELOPER = "developer"; private static final String LARGE_IMAGE = "large_image"; private static final String SMALL_IMAGE = "small_image";
Finally, here is what my xml looks like.
058537d9039047a39876eb403515d13a.gif 76822fef4f14417fb43c23df999ff73b.JPG Handango Jamdat Mobile eb59ed645fe4469f85456241399e9750.3gp
Full source is here.
Please note that the xml is not included. This example will only really work on your device and not on an emulator as the file needs to exist in the sdcard. You will need to modify the path to get this example to work.
5 comments on “parsing xml from the sdcard using XmlPullParser”