parsing xml from the sdcard using XmlPullParser

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.





  

    

      

      google

      058537d9039047a39876eb403515d13a.gif

      

    

    

      

      google

      

      

    

    

      

      google

      76822fef4f14417fb43c23df999ff73b.JPG

      

    

  

  

    

      

      Handango

      

      

    

    

      

      Jamdat Mobile

      

      

    

  

  

    

    

  

  

    

      

      

      

      

        

          3:50

          1

          

        

        

          4:20

          2

          

        

        

          4:53

          3

          

        

      

    

    

      

      

      

      

        

          2:28

          1

          

        

        

          1:36

          2

          

        

      

    

  

  

    

      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”

  1. gman413

    Since many Android XML parsing examples focus only on stream input for XML feeds, your example was exactly what I needed to get going, thank you for posting this!

  2. noslen

    Thanks gman413, i’m glad you found it helpful :)

  3. forum6691

    The following code
    // set the input for the parser using an InputStreamReader
    xpp.setInput(new InputStreamReader(fis));

    generate a java error
    The method setInput(InputStream, String) in the type XmlPullParser is not applicable for the arguments (InputStreamReader, null)

    because the type InputStreamReader are not acceptable for The method setInput .

  4. forum6691

    I correct ma last post
    I have used setInput (Inputstream, String). In this case there is an error. If you use only one parameters it’s good.

    Thank you very much. I’m searching an example like this ( which is ok) all this day on the net.

Leave a Reply

Your email address will not be published. Required fields are marked *