View RSS Feed

My Java Tips

XML Pull Parsing (Demo)

Rate this Entry
by , 11-22-2011 at 05:58 PM (2629 Views)
XML Pull Parsing makes parsing 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/or...ullParser.html

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();
         factory.setNamespaceAware(true);
         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 = xpp.next();
         }
     }
 }
Output:


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 del.icio.us Submit "XML Pull Parsing (Demo)" to StumbleUpon Submit "XML Pull Parsing (Demo)" to Google

Categories
XML

Comments