4-8. Parsing XML
Problem
Your application needs to parse responses, from an API or other source, that are formatted as XML.
Solution
(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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access