XML Pull Parsing (Demo)
25 April 2008XML Pull Parsing makes marsing XML documents easier and efficient. This post introduces this API.
You may get the required API from http://www.xmlpull.org/.
Java docs are available at: http://www.xmlpull.org/v1/doc/api/org/xmlpull/v1/XmlPullParser.html
Let me present an example of parsing XML using XML Pull Parsing.
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(); } } }
Output:
Start document Start tag foo Text Hello World! End tag foo
Related Posts:
Top Of Page | Trackback
If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.
It will look like this: XML Pull Parsing (Demo)