mlpull.org/">XMLPULL V1 API (visit this website to learn more about API and its implementations).
There are following different kinds of parser depending on which features are set:
- non-validating parser as defined in XML 1.0 spec when FEATURE_PROCESS_DOCDECL is set to true
- validating parser as defined in XML 1.0 spec when FEATURE_VALIDATION is true (and that implies that FEATURE_PROCESS_DOCDECL is true)
- when FEATURE_PROCESS_DOCDECL is false (this is default and if different value is required necessary must be changed before parsing is started) then parser behaves like XML 1.0 compliant non-validating parser under condition that no DOCDECL is present in XML documents (internal entities can still be defined with defineEntityReplacementText()). This mode of operation is intended for operation in constrained environments such as J2ME.
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 whitespaces, use nextToken() instead)
- END_TAG
- An end tag was read
- END_DOCUMENT
- No more events are available
after first next() or nextToken() (or any other next*() method) is called user application can obtain XML version, standalone and encoding from XML declaration in following ways:
- version: getProperty("http://xmlpull.org/v1/doc/properties.html#xmldecl-version") returns String ("1.0") or null if XMLDecl was not read or if property is not supported
- standalone: getProperty("http://xmlpull.org/v1/doc/features.html#xmldecl-standalone") returns Boolean: null if there was no standalone declaration or if property is not supported otherwise returns Boolean(true) if standalone="yes" and Boolean(false) when standalone="no"
- encoding: obtained from getInputEncoding() null if stream had unknown encoding (not set in setInputStream) and it was not declared in XMLDecl
A minimal example for using this API may look as follows:
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 != xpp.END_DOCUMENT) { if(eventType == xpp.START_DOCUMENT) { System.out.println("Start document"); } else if(eventType == xpp.END_DOCUMENT) { System.out.println("End document"); } else if(eventType == xpp.START_TAG) { System.out.println("Start tag "+xpp.getName()); } else if(eventType == xpp.END_TAG) { System.out.println("End tag "+xpp.getName()); } else if(eventType == xpp.TEXT) { System.out.println("Text "+xpp.getText()); } eventType = xpp.next(); } } }
The above example will generate the following output:
Start document Start tag foo Text Hello World! End tag foo
For more details on API usage, please refer to the quick Introduction available at http://www.xmlpull.org
@see #defineEntityReplacementText
@see #getName
@see #getNamespace
@see #getText
@see #next
@see #nextToken
@see #setInput
@see #FEATURE_PROCESS_DOCDECL
@see #FEATURE_VALIDATION
@see #START_DOCUMENT
@see #START_TAG
@see #TEXT
@see #END_TAG
@see #END_DOCUMENT
@author Stefan Haustein
@author Aleksander Slominski