Version 1.1
Copyright © 2011, 2012 Lars Vogel
22.04.2012
| Revision History | ||
|---|---|---|
| Revision 0.1 | 05.12.2011 | Lars Vogel |
| Created | ||
| Revision 0.2 - 1.1 | 07.12.2011 - 22.04.2012 | Lars Vogel |
| Created | ||
Table of Contents
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.
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>
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 doesn't enclose any content is know as an "empty tag". For example: <flag/>
Comments in XML are defined as: <! COMMENT>
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.
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(); } } }
Before posting questions, please see the vogella FAQ. If you have questions or find an error in this article please use the www.vogella.com Google Group. I have created a short list how to create good questions which might also help you.
Introduction to Android Development
Android Location API and Google Maps
Crest Framework to access rest services, works also on Android
Eclipse RCP Training (German) Eclipse RCP Training with Lars Vogel
Android Tutorial Introduction to Android Programming
GWT Tutorial Program in Java and compile to JavaScript and HTML
Eclipse RCP Tutorial Create native applications in Java
JUnit Tutorial Test your application
Git Tutorial Put everything you have under distributed version control system