View RSS Feed

My Java Tips

XML Pull Parsing (Demo)

Rate this Entry
by , 11-22-2011 at 05:58 PM (3982 Views)
XML Pull Parsing makes parsing XML documents easier and efficient. This post introduces this API.

You may get the required API from
Java docs are available at:

Let me present an example of parsing XML using XML Pull Parsing.

Java Code:
public class SimpleXmlPullApp

     public static void main (String args[])
         throws XmlPullParserException, IOException
         XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
         XmlPullParser xpp = factory.newPullParser();

         xpp.setInput( new StringReader ( "Hello World!" ) );
         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 =;

Start document
Start tag foo
Text Hello World!
End tag foo

Submit "XML Pull Parsing (Demo)" to Facebook Submit "XML Pull Parsing (Demo)" to Digg Submit "XML Pull Parsing (Demo)" to Submit "XML Pull Parsing (Demo)" to StumbleUpon Submit "XML Pull Parsing (Demo)" to Google