Home » Kotlin Android XML Parsing using SAX Parser

Kotlin Android XML Parsing using SAX Parser

by Online Tutorials Library

Kotlin Android XML Parsing using SAX Parser

XML document is commonly used to share the data on the internet. The data provided in the XML format are able to update frequently and parsing them is a common task for network-based apps.

In Android, there are three types of XML parsers to parse the XML data and read them in android applications, these are:

  1. DOM Parser
  2. SAX Parser
  3. XMLPullParser

In Android, SAX (Simple API for XML) is widely used API for XML parsing. The SAX parser will examines an XML document, character by character and translates it into a series of events such as startElement(), endElement() and characters(). To read and parse the XML data using SAX parser, we need to create an instance of SAXParserFactory, SAXParser and DefaultHandler objects in android applications.

Example of XML Parsing using SAX Parser

In this example, we will parse the XML data using SAX parser and display them into ListView.

activity_main.xml

Add the ListView in the activity_main.xml layout.

empdetail.xml

Create the XML document empdetail.xml in assets directory to parse the data using SAX parser.

custom_list.xml

Create a custom layout to display the list of data into ListView.

MainActivity.kt

Add the following code to read and parse the XML data using SAX parser. Create the instance of SAXParserFactory, SAXParser and DefaultHandler objects.

The HashMap<String, String> is used to read the data from XML document and add them into ArrayList().

Output:

Kotlin Android XML Parsing using SAX Parser

Next Topic#

You may also like