4-8. Parsing XML


Your application needs to parse responses, from an API or other source, that are formatted as XML.


(API Level 1)

Implement a subclass of org.xml.sax.helpers.DefaultHandler to parse the data using event-based SAX (Simple API for XML). Android has three primary methods you can use to parse XML data: DOM (Document Object Model), SAX, and Pull. The simplest of these to implement, and the most memory-efficient, is the SAX parser. SAX parsing works by traversing the XML data and generating callback events at the beginning and end of each element.

How It Works

To describe this further, let’s look at the format of the XML that is returned when requesting an RSS/Atom news feed (see Listing 4-27).

Listing 4-27. RSS ...

Get Android Recipes: A Problem-Solution Approach, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.