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.
-->There are two key methods: next() and nextToken(). While next() provides access
to high level parsing events, nextToken() allows access to lower level tokens.
-->The current event state of the parser can be determined by calling the
getEventType() method. Initially, the parser is in the START_DOCUMENT state.
-->The method next() advances the parser to the next event.
The int value returned from next determines the current parser state and is identical
to the value returned from following calls to getEventType ().
The following event types are seen by next():
---------------------------------------------
START_TAG
An XML start tag was read.
TEXT
Text content was read; the text content can be retrieved using the getText() method.
(when in validating mode next() will not report ignorable whitespace, use nextToken() instead)
END_TAG
An end tag was read
END_DOCUMENT
No more events are available
Example XmlPullParser:
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();
}
}
}
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.
-->There are two key methods: next() and nextToken(). While next() provides access
to high level parsing events, nextToken() allows access to lower level tokens.
-->The current event state of the parser can be determined by calling the
getEventType() method. Initially, the parser is in the START_DOCUMENT state.
-->The method next() advances the parser to the next event.
The int value returned from next determines the current parser state and is identical
to the value returned from following calls to getEventType ().
The following event types are seen by next():
---------------------------------------------
START_TAG
An XML start tag was read.
TEXT
Text content was read; the text content can be retrieved using the getText() method.
(when in validating mode next() will not report ignorable whitespace, use nextToken() instead)
END_TAG
An end tag was read
END_DOCUMENT
No more events are available
Example XmlPullParser:
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();
}
}
}
No comments:
Post a Comment