
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Parse XML in Android Using Kotlin with XMLPullParser
This example demonstrates how to use XMLPullParser to parse XML in Android using Kotlin.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
Step 2 − Add the following code to res/layout/activity_main.xml.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <ListView android:id="@+id/listView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:dividerHeight="1dp" /> </LinearLayout>
Step 3 − Create a Layout resource file (row.xml) and add the following code −
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="4dp"> <TextView android:id="@+id/tvName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/background_dark" android:textSize="16sp" android:textStyle="bold" /> <TextView android:id="@+id/tvDesignation" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tvName" android:layout_marginTop="7dp" android:textColor="#343434" android:textSize="12sp" /> <TextView android:id="@+id/tvLocation" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/tvDesignation" android:layout_alignBottom="@+id/tvDesignation" android:layout_alignParentEnd="true" android:textColor="@android:color/background_dark" android:textSize="16sp" /> </RelativeLayout>
Step 5 − Create a new asset folder, and inside the asset folder create a Android resource file (model.xml) and add the following code −
<?xml version="1.0" encoding="utf-8"?> <resources> <users> <user> <name>Sehwag</name> <designation>Vice Captain</designation> <loation>Delhi</loation> </user> <user> <name>Ashwin</name> <designation>Off Spin Bowler</designation> <loation>Chennai</loation> </user> <user> <name>Dhoni</name> <designation>Captain</designation> <loation>Ranchi</loation> </user> </users> </resources>
Step 6 − Add the following code to src/MainActivity.kt
import android.os.Bundle import android.widget.ListAdapter import android.widget.ListView import android.widget.SimpleAdapter import androidx.appcompat.app.AppCompatActivity import org.xmlpull.v1.XmlPullParser import org.xmlpull.v1.XmlPullParserException import org.xmlpull.v1.XmlPullParserFactory import java.io.IOException class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) title = "KotlinApp" try { val userList = ArrayList<java.util.HashMap<String?, String?>>() var user: HashMap<String?, String?>? = HashMap() val lv: ListView = findViewById(R.id.listView) val inputStream = assets.open("model.xml") val parserFactory: XmlPullParserFactory = XmlPullParserFactory.newInstance() val parser: XmlPullParser = parserFactory.newPullParser() parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true) parser.setInput(inputStream, null) var tag: String? var text = "" var event = parser.eventType while (event != XmlPullParser.END_DOCUMENT) { tag = parser.name when (event) { XmlPullParser.START_TAG -> if (tag == "user") user = HashMap() XmlPullParser.TEXT −> text = parser.text XmlPullParser.END_TAG −> when (tag) { "name" −> user!!["name"] = text "designation" −> user!!["designation"] = text "location" −> user!!["location"] = text "user" −> if (user != null) userList.add(user) } } event = parser.next() } val adapter: ListAdapter = SimpleAdapter(this@MainActivity, userList, R.layout.row, arrayOf("name", "designation", "location"), intArrayOf(R.id.tvName, R.id.tvDesignation, R.id.tvLocation)) lv.adapter = adapter } catch (e: IOException) { e.printStackTrace() } catch (e: XmlPullParserException) { e.printStackTrace() } } }
Step 6 − Add the following code to androidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.q11"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click the Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen