Cdacians

Cdacians
Cdacians

Thursday, 23 August 2012

XML processing with Android


XML processing with Android
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).

1. XML Introduction

1.1. 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.

1.2. XML Example

The following is a valid, well-formated XML file.
<?xml version="1.0"?>
<!-- This is a comment -->
<address>
  <name>Lars </name>
  <street> Test </street>
  <telephon number= "0123"/>
</address> 

1.3. XML Elements

A XML document always starts with a prolog which describes the XML file. This prolog can be minimal, e.g. <?xml version="1.0"?> or can contain other information, e.g. the encoding, e.g. <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
A tag which does not enclose any content is know as an "empty tag". For example: <flag/>
Comments in XML are defined as: <! COMMENT>

2. XML processing in Android

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.

3. Example XmlPullParser

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();
         factory.setNamespaceAware(true);
         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();
         }
     }
 } 

4. Thank you

Please help me to support this article:

Thanks
akm
www.cdacians.com

No comments:

Post a Comment