XML Pull Parsing (Demo)

25 April 2008

XML 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

del.icio.us:XML Pull Parsing (Demo)  digg:XML Pull Parsing (Demo)  spurl:XML Pull Parsing (Demo)  wists:XML Pull Parsing (Demo)  simpy:XML Pull Parsing (Demo)  newsvine:XML Pull Parsing (Demo)  blinklist:XML Pull Parsing (Demo)  furl:XML Pull Parsing (Demo)  reddit:XML Pull Parsing (Demo)  fark:XML Pull Parsing (Demo)  blogmarks:XML Pull Parsing (Demo)  Y!:XML Pull Parsing (Demo)  smarking:XML Pull Parsing (Demo)  magnolia:XML Pull Parsing (Demo)  segnalo:XML Pull Parsing (Demo)  gifttagging:XML Pull Parsing (Demo)

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)

Leave a Reply