2

I am trying to use XMLPullParser but I cannot find any useful tutorials. Based off of the instructions on http://xmlpull.org/ I need to download an implementation of XMLPullParser as a jar file and then add it to my class path. However I cannot find any link to any jar file that works. Does anyone know where I might be able to find a jar file I can download.

Thanks

1
  • There are .jar files for download if you click "Download Implementation". What's the problem? Commented Feb 6, 2015 at 19:48

1 Answer 1

2

Ok, here it is for you.

From the official doc :

XmlPull API Implementations:

  1. XNI 2 XmlPull
  2. XPP3/MXP1
  3. KXML2

Here i use KXML2.

Steps :

  1. Download KXML2 jar file from here.
  2. Create a new java project

enter image description here

  1. Create a new class

enter image description here

  1. Right click the java project -> Properties -> Java Build path -> Libraries -> Add external jar's -> Add downloaded kxml2 jar file.

enter image description here

  1. Java code

    import java.io.IOException;
    import java.io.StringReader;
    import org.xmlpull.v1.XmlPullParser;
    import org.xmlpull.v1.XmlPullParserException;
    import org.xmlpull.v1.XmlPullParserFactory;
    
    public class XmlPullparserBasic {
    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.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();
        }
    
        System.out.println("End document");
    
      }
    
    }
    

Output :

enter image description here

Hope it helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.