如何在Android中使用Kotlin和XMLPullParser解析XML?
此示例演示了如何使用Kotlin在Android中使用XMLPullParser解析XML。
步骤1 − 在Android Studio中创建一个新项目,转到文件 ⇒ 新建项目,并填写所有必需的详细信息以创建新项目。
步骤2 − 将以下代码添加到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>
步骤3 − 创建一个布局资源文件(row.xml)并添加以下代码 −
<?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>
步骤5 − 创建一个新的asset文件夹,并在asset文件夹内创建一个Android资源文件(model.xml)并添加以下代码 −
<?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>
步骤6 − 将以下代码添加到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()
}
}
}步骤6 − 将以下代码添加到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>
让我们尝试运行您的应用程序。我假设您已将您的实际Android移动设备连接到您的计算机。要从Android Studio运行应用程序,请打开您项目中的一个活动文件,然后单击工具栏中的运行图标
。选择您的移动设备作为选项,然后检查您的移动设备,它将显示您的默认屏幕。

广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP