📅  最后修改于: 2021-01-05 08:33:36             🧑  作者: Mango
XML文档通常用于在Internet上共享数据。 XML格式提供的数据能够经常更新,并且解析它们是基于网络的应用程序的常见任务。
在Android中,有三种类型的XML解析器可以解析XML数据并在android应用程序中读取它们,它们是:
在Android中, SAX(XML的简单API)被广泛用于XML解析的API。 SAX解析器将通过字符检查XML文档,字符,并将其转换成一系列的事件,诸如的startElement(),的endElement()和字符()。要使用SAX解析器读取和解析XML数据,我们需要在android应用程序中创建SAXParserFactory,SAXParser和DefaultHandler对象的实例。
在此示例中,我们将使用SAX解析器解析XML数据并将其显示在ListView中。
在activity_main.xml布局中添加ListView。
在资产目录中创建XML文档empdetail.xml,以使用SAX解析器解析数据。
Sachin Kumar
50000
Developer
Rahul Kumar
60000
Team Leader
John Mike
70000
Manager
Ajay Kumar
45000
Developer
Toni Nayer
55000
Marketing
Mr Bony
42000
Sales
Raj Kumar
30000
Production
Rahul Kumar
60000
Team Leader
John Mike
70000
Manager
Sachin Kumar
50000
Developer
Rahul Kumar
60000
Team Leader
John Mike
70000
Manager
创建一个自定义布局以将数据列表显示到ListView中。
添加以下代码,以使用SAX解析器读取和解析XML数据。创建SAXParserFactory,SAXParser和DefaultHandler对象的实例。
HashMap
package example.javatpoint.com.kotlinxmlparsingusingsaxparser
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import org.xml.sax.helpers.DefaultHandler
import javax.xml.parsers.SAXParserFactory
import android.widget.ListView
import android.widget.SimpleAdapter
import org.xml.sax.SAXException
import java.io.IOException
import java.util.ArrayList
import java.util.HashMap
import javax.xml.parsers.ParserConfigurationException
import javax.xml.parsers.SAXParser
class MainActivity : AppCompatActivity() {
internal var empList = ArrayList>()
internal var empData = HashMap()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val lv:ListView = findViewById(R.id.listView)
try {
//instancing the SAXParserFactory class
val parserFactory:SAXParserFactory = SAXParserFactory.newInstance()
//instancing the SAXParser class
val saxParser:SAXParser = parserFactory.newSAXParser()
val defaultHandler= object : DefaultHandler() {
var currentValue = ""
var currentElement = false
//overriding the startElement() method of DefaultHandler
override fun startElement(uri: String, localName: String, qName: String, attributes: org.xml.sax.Attributes) {
currentElement = true
currentValue = ""
if (localName == "employee") {
empData = HashMap()
}
}
//overriding the endElement() method of DefaultHandler
override fun endElement(uri: String, localName: String, qName: String) {
currentElement = false
if (localName.equals("name", ignoreCase = true))
empData["name"] = currentValue
else if (localName.equals("salary", ignoreCase = true))
empData["salary"] = currentValue
else if (localName.equals("designation", ignoreCase = true))
empData["designation"] = currentValue
else if (localName.equals("employee", ignoreCase = true))
empList.add(empData)
}
//overriding the characters() method of DefaultHandler
override fun characters(ch: CharArray, start: Int, length: Int) {
if (currentElement) {
currentValue = currentValue + String(ch, start, length)
}
}
}
val istream = assets.open("empdetail.xml")
saxParser.parse(istream, defaultHandler)
//creating Adapter class to access the custom list
val adapter = SimpleAdapter(this@MainActivity, empList, R.layout.custom_list, arrayOf("name", "salary", "designation"), intArrayOf(R.id.name, R.id.salary, R.id.designation))
lv.adapter = adapter
} catch (e: IOException) {
e.printStackTrace()
} catch (e: ParserConfigurationException) {
e.printStackTrace()
} catch (e: SAXException) {
e.printStackTrace()
}
}
}
输出: