XML Parsing in Android using SAX Parser
Generally, XML (Extensible Mark-up Language) is a commonly used data exchange format to interchange servers’ data. In Android, SAX stands for Simple API for XML and is a widely used API for XML parsing. Like the DOM parser, the SAX parser is also used to perform in-memory operations to parse the XML document, but it consumes less memory than the DOM parser. The main advantage of a SAX parser over a DOM parser is that one can instruct the SAX parser to stop midway through a document without losing any collected data. The XML file that contains the information to be extracted includes the following four main components:
- Prolog: The XML file will start with a prolog. Prolog contains the information about a file, which is available in the first line.
- Events: Events such as document start and end, tag start and end, etc. are contained in the XML file
- Text: It is a simple text present in between the opening and closing XML tag elements.
- Attributes: They are the additional properties of a tag present within the label.
Note that we are going to implement this project using the Kotlin language. One may also perform XML Parsing in another two ways. Please refer to the below articles:
What we are going to do?
- We need to have an XML file with some information so that we would make one. Place this file under the assets folder. This file is called and would be parsed.
- We want to show this data in the form of a list to implement a ListView.
- SAX parser scrutinizes an XML file character by character, and translates the XML file into a series of events, such as startElement(), endElement() and characters().
- A ContentHandler object will process these events to perform the appropriate action. The parse() method will send the events to the content object to deals with them.
- Create an instance of SAXParserFactory, SAXParser, and DefaultHandler objects in android applications to read and parse the XML data using SAX parser in Android.
- Using a ListAdapter, the data is sent to the ListView where it is displayed on the screen.
To parse an XML file using a SAX parser in Android, follow the following steps:
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Create an assets folder
Create an assets folder under the main folder in the Project Layout. Create an Android Resource File in this folder, where we shall put the information in the form of XML. Name this file as userdetails.xml. For doing so refer to the following steps:
Click on Project as shown on the left side of the below image.
Expand until you find the main folder, right-click on it, go to New > Folder > Assets Folder
Then just click on the Finish button.
Now the asset folder is created successfully. Right-Click on the Assets Folder > New > Android Resource FIle
Give it name Information, change type to XML, and finish.
Note: Sometimes, right-clicking on the Assets folder and creating an Android Resource File creates a file in the res folder. If this happens, cut our file and paste it directly into the assets folder. This happens due to some internal settings.
Paste this information which is in the form of XML, that is to be displayed in the userdetails.xml file. Below is the code for the userdetails.xml file.
Step 3: Working with the activity_main.xml file
Now go to the activity_main.xml file which represents the UI of the application. Create a ListView as shown. Below is the code for the activity_main.xml file.
Step 4: Create another layout file
Go to app > res > layout > right-click > New > Layout Resource File and name the file as list. list.xml file is used to show the data in the ListView. Below is the code for the list.xml file.
Step 5: Working with the MainActivity.kt file
Finally, go to the MainActivity.kt file, and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.