JSON is the most popular data exchange format for mobile, but Android developers also sometimes need to work with XML formatted data files. As with JSON, the Android SDK includes a few standardized tools. You can use DOM-style parsing, since it’s part of the standard JDK and is included in Android, but DOM tends to be memory-heavy since it has to store an entire XML document in memory. Instead, it’s recommended that you use a streaming API to read from XML files, and a very lightweight API is included in the SDK: it’s named XMLPullParser.
- [Voiceover] JSON is the most popular…data exchange format for mobile,…but Android developers also sometimes need…to work with XML formatted data files.…As with JSON, the Android SDK…includes a few standardized tools…based on tools that are either included in…the Oracle JDK…or on open source projects.…You could, if you like, use DOM-style parsing.…From the homepage of the API docs,…I'll search for javax.xml.parsers…and I'll see that there are tools available…for DOM-style parsing…and for SAX, or the Simple API for XML.…
DOM, or Document Object Model,…tends to be memory heavy…since you have to store an entire XML document in memory,…but there's another option entirely…called the XmlPullParser.…This is based on an open source project,…and again, is included in the core Android SDK.…I'll show you how to use the XmlPullParser…to parse a file that's stored in a project's assets folder.…You'll be able to use the same sort of coding patterns…to parse XML that you retrieve from a source on the web.…
I'm working in the project ReadXML.…
Watch these Java tutorials to learn smarter, more efficient methods for Android app development.
- Preparing the development environment
- Packaging and running Android apps
- Optimizing Java code for Android
- Implementing event handler interfaces
- Defining custom callback methods
- Working with the Android SDK's Java packages
- Storing data
- Reading text files
- Parsing JSON and XML data
- Managing device sensors
- Playing audio