Kotlin Android XML Parsing using DOM Parser
XML document is commonly used to share the data on the internet. The data provided in 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:
- DOM Parser
- SAX Parser
- XMLPullParser
Android DOM (Document Object Model) parser uses an object-based approach to create and parse the XML files in android applications. The DOM parser loads the XML file into memory to parse the XML document. Due to this reason, it consumes more memory.
Example of XML Parsing using DOM Parser
In this example, we parse XML data 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 DOM 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 DOM parser. Create the instance of DocumentBuilderFactory, DocumentBuilder and Document objects.
The HashMap<String, String> is used to read the data from XML document and add them into ArrayList().
Output: