Tutorial details

XML Processing with the XmlPullParser | App Code for Sale | Preview

XML Processing with the XmlPullParser | Android Tutorial

This article describes how to process XML with Android. It is based on Eclipse 3.7, Java 1.6 and Android 4.0

Overview PAGE TOP

This article describes how to process XML with Android. It is based on Eclipse 3.7, Java 1.6 and Android 4.0 (Ice Cream Sandwich).

http://www.vogella.de under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Germany (CC BY-NC-SA 3.0) licence.

XML Introduction PAGE TOP

XML Overview

XML stands for Extensible Markup Language and was defined 1998 by the World Wide Web Consortium (W3C).

A XML document consists out of elements, each element has a start tag, content and an end tag. A XML document must have exactly one root element, e.g. one tag which encloses the remaining tags. XML makes a difference between capital and non-capital letters.

  • A XML file is required to be "well-formated".
  • A "well-formated" XML file must apply to the following conditions:
  • A XML document always starts with a prolog (see below for an explanation of what a prolog is)
  • Every opening tag has a closing tag.
  • All tags are completely nested.

A XML file is called valid, if it is well-formated and if it is contains a link to a XML schema and is valid according to the schema.

Using XML has the following advantages vs. using a binary or unstructured format:

  • XML is plain text.
  • XML represents data without defining how the data should be displayed.
  • XML can be transformed into other formats via XSL.
  • XML can be easily processed via standard parsers.
  • XML files are hierarchical.

XML Example

The following is a valid, well-formated XML file.

<?xml version="1.0"?>
<!-- This is a comment -->
  <name>Lars </name>
  <street> Test </street>
  <telephon number= "0123"/>

XML Elements

A XML document always starts with a prolog which describes the XML file. This prolog can be minimal, e.g. or can contain other information, e.g. the encoding, e.g.

A tag which does not enclose any content is know as an "empty tag". For example: <flag/>

Comments in XML are defined as: <! COMMENT>

XML processing in Android PAGE TOP

The Java programming language provides several standard libraries for processing XML files. The SAX and the DOM XML parsers are also available on Android.

The SAX and DOM parsers API is on Android the same as in standard Java. SAX and DOM have their limitations, therefore it is not recommended to use them on Android. Therefore this tutorial does not give an example for the usage of this library.

The Java standard provides also the Stax parser. This parser is not part of the Android platform.

Android provides for XML parsing and writing the XmlPullParser class. This parser is not available in standard Java but is similar to the Stax parser. The parser is hosted at http://www.xmlpull.org/ .

On Android it is recommended to use the XmlPullParser. It has a relatively simple API compared to SAX and DOM and is fast and requires less memory then the DOM API.

Example XmlPullParser PAGE TOP

The Javadoc of XmlPullParser gives a nice example how you can use this library.

import java.io.IOException;
 import java.io.StringReader;

 import org.xmlpull.v1.XmlPullParser;
 import org.xmlpull.v1.XmlPullParserException.html;
 import org.xmlpull.v1.XmlPullParserFactory;

 public class SimpleXmlPullApp

     public static void main (String args[])
         throws XmlPullParserException, IOException
         XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
         XmlPullParser xpp = factory.newPullParser();

         xpp.setInput(new StringReader ("<foo>Hello World!</foo>"));
         int eventType = xpp.getEventType();
         while (eventType != XmlPullParser.END_DOCUMENT) {
          if(eventType == XmlPullParser.START_DOCUMENT) {
              System.out.println("Start document");
          } else if(eventType == XmlPullParser.END_DOCUMENT) {
              System.out.println("End document");
          } else if(eventType == XmlPullParser.START_TAG) {
              System.out.println("Start tag "+xpp.getName());
          } else if(eventType == XmlPullParser.END_TAG) {
              System.out.println("End tag "+xpp.getName());
          } else if(eventType == XmlPullParser.TEXT) {
              System.out.println("Text "+xpp.getText());
          eventType = xpp.next();
0 Comments Leave a comment

Please login in order to leave a comment.

Newest first

to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.