Parsing XML with XmlPullParser

XmlPullParser is an interface that provides parsing functionality. The below code is an example of XmlPullParser usage.

We will parse the Youtube feed that contains the most highly rated YouTube video, looking for “<title>” tag and extracting the text inside.

URL Feed:

//We will put the data into a StringBuilder
StringBuilder builder=new StringBuilder();

URL url = new URL("");

XmlPullParserFactory factory=XmlPullParserFactory.newInstance();
XmlPullParser xpp=factory.newPullParser();

xpp.setInput(getInputStream(url), "UTF_8");

int eventType=xpp.getEventType();
  // Looking for a start tag
    //We look for "title" tag in XML response
      //Once we found the "title" tag, add the text it contains to our builder

And here’s the implementation of getInputStream() method:

public InputStream getInputStream(URL url) {
  try {
    return url.openConnection().getInputStream();
  } catch (IOException e) {
    return null;

6 thoughts on “Parsing XML with XmlPullParser

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s