JSON is become a very widespread format for the Web Services but in some cases, you would have to parse data in the good old XML format. In that tutorial, you are going to learn how to parse a XML file on Android.

Note that there are various XML parses available in the Android SDK in standard. Thus, you can use the following solutions :

  • XMLPullParser API
  • DOM Parser API
  • SAX Parser API

As usual, each parser has its advantages and drawbacks.

The recommendation on Android is to use the XMLPullParser API which consumes less memory than the DOM Parser API which loads the complete XML file in memory before parsing it.

So, in this tutorial, we will use the XMLPullParser API. Note that you can also discover this tutorial in video on YouTube :

 

Creating a simple User Interface

To display the parsed XML data, we need to create a simple User Interface. It will consist in a simple TextView centered on the screen :

 

Defining XML data to parse

Then, we define some XML data to parse inside a file under the assets directory of our Android project. This file will be named data.xml and will contain some NBA players with data like name, age and position played :

 

Creating a Player POJO

To map the data for each player, we create a Player POJO (Plain Old Java Object) :

 

Preparing the XML Parser

The first step is to load the XML file from the assets of our Android Application. For that, we define a parseXML method. We create a new instance of the XMLPullParserFactory class. Then, we create a new XMLPullParser instance by calling the newPullParser method of the XMLPullParserFactory got previously.

We got an InputStream on the XML file by calling the open method of the AssetManager instance. With that instance, we can set the input on the XMLPullParser instance. It gives us the following code :

 

Parsing the XML data

Now, we need to parse the XML data. The parsing will be processed in the processParsing method. We start by getting the current event type from the parser by calling the getEventType method.

Then, we enter in a loop while the event type is different of the XmlPullParser.END_DOCUMENT constant. In the loop, when we meet a XmlPullParser.START_TAG event type, we get the name of the current tag by calling the getName method.

When we have a player tag, we create a new Player instance and we add it in an ArrayList of Player. This list will be used to store all the players read from the XML file.

If the current player is not null, we are reading data for a player. We have to check if the tag is name, age or position. For each case, we call the nextText method of the parser to get the value associated to the tag. Then, we set the value on the current player.

At the end of the loop, we have stored all the players from the XML file on the ArrayList of Player. Note that just before the end of the loop, you need to call the next method of the parser to pass to the following event.

It gives us the following code :

 

Displaying the XML data

The last step is also the simplest. We need to display the players in our TextView via the printPlayers called at the end of the processParsing method.

In that method, we iterate on the ArrayList of Player and for each player we add its properties (name, age and position) on a StringBuilder instance. Finally, we display the text on the TextView by calling its setText method with the value of the StringBuilder instance.

It gives us the following complete code for the MainActivity :

 

Our App in Action

Best part of the tutorial is coming since it’s time to put our App in Action. When, you will launch the Application, you should have the following result :

That’s all for that tutorial.

To discover more tutorials on Android development, don’t hesitate to subscribe to the SSaurel’s Channel on YouTube :