Android XML Parser Example using XMLPullParser

Parsing XML in Android is pretty simple and easy by using XMLPullParser. There are different types of parsers are available in Android such as Dom, SAS and XMLPullParser. XMLPullParser is the most efficient, easy and the recommended method. In this Android XML Parser example, We will create a small Android application project to demonstrate all steps involved in the development. We will make a network call to a remote server by using AsyncTask to get XML data and then parse the XML data using XMLPullParser.

What will you learn in this tutorial

How to parse XML by using XMLPullParser.
How to use AsyncTask class to make the network call.
How to use HttpURLConnection to connect to a remote server.

Android XML Parser – Implementation

Create a new project in Android Studio with default settings and name it whatever you want to name. the Project will have only a single activity which is MainActivity in my case.

Add Internet permission in Android Manifest file.
AndroidManifest.xml

Create a new java class file and name it HttpHandler.java. Here is the complete code of HttpHandler.java.

We will connect to the server by using HttpURLConnection class. We have created two methods CallServer() and StreamToString() in this class.

CallServer() method will connect to the server by using HttpURLConnection class and returnt he data in the form of inputstream. StreamToString() method will convert inputstream into string format.

XML struture to be used in this Android XMLPullParser Example

In MainActivity, We will create an AsyncTask subclass to make the network call. We will pass server URL to CallServer() method. It will return the inputstream. The method StreamToString() will convert the inputstream into string format. We will pass returned XML string to ParseXML() method.

The method ParseXML() will parse the XML data. We have implemented XmlPullParser in this method. We can extract the data of all fields or only those required and skip the rest.

Read further about XMLPullParser on official website of Android.

android xml parser

Finally, we will run the application. As we see in the logs that it has printed the extracted fields from the XML and ignore the rest.

android xml parser output

Android XML Parser Tutorial – Video


Here is the video demonstration of project structure and output.

I hope you will like this tutorial. Subscribe to our Newsletter to get new articles and tutorials in your inbox as soon as they published. You can connect with us on Facebook and also subscribe to our Youtube Channel

Check Also

asynctask kotlin example

Kotlin AsyncTask Tutorial with Example

Here is an example of implementing AsyncTask in Kotlin. I will create an Android application …