[XML parsing] Pull parsing XML

1. Briefly describe the features of Pull parsing XML

Pull parsing XML is similar to Sax parsing XML, the difference is that Sax scans and parses documents automatically, while Pull scans and parses documents requires us to manually call next()methods. Each time the corresponding content is scanned, a corresponding event will be reported, and we need to write our own code logic to process it according to the corresponding event.
Among them, there are five events for Pull parsing XML documents:

  • START DOCUMENT: Document start event, the document has not scanned any content;
  • START_TAG: tag start event, the parser is in the start tag;
  • TEXT: text event, the parser is at the node content;
  • END_TAG: tag start event, the parser is at the end tag;
  • END_DOCUMENT: Document end event, the parser parsing the document ends.

2. Give an example

Parse the following persons.xml

<?xml version="1.0" encoding="utf-8" ?>
<persons>
    <person id="p1">
        <name>小明</name>
        <age>18</age>
    </person>
    <person id="p2">
        <name>小华</name>
        <age>23</age>
    </person>
</persons>

pull parsing xml logic class

import android.text.TextUtils
import com.mapc.demo.pojo.Person
import org.xmlpull.v1.XmlPullParser
import org.xmlpull.v1.XmlPullParserFactory
import java.io.InputStream

class XmlPullParser{

    /**
     * 放在里面,类似Java静态成员、静态方法,可以直接:类名.xx
     */
    companion object {

        private lateinit var personList:ArrayList<Person>
        private var currentTag:String?=null//解析的当前标签
        private var nodeName:String?=null//记录开始解析的节点标签
        private var person:Person?=null//一个节点的封装对象

        fun parseXmlForPersonList(inputStream:InputStream,nodeName:String):ArrayList<Person>?{

            this.nodeName = nodeName

            //创建解析器工厂
            var factory = XmlPullParserFactory.newInstance()
            //创建解析器
            var parser = factory.newPullParser()
            //解析器设置解析文档流
            parser.setInput(inputStream,"utf-8")
            //获取第一个事件类型
            var eventType = parser.eventType

            while (eventType != XmlPullParser.END_DOCUMENT){
                when(eventType){
                    XmlPullParser.START_DOCUMENT ->{
                        personList = ArrayList<Person>()
                    }
                    XmlPullParser.START_TAG ->{
                        if (!TextUtils.isEmpty(parser.name)){
                            println("--------------标签${parser.name}--------------")
                            this.currentTag = parser.name
                            if (this.currentTag == this.nodeName) {
                                person = Person(null, null, null)
                                if (parser.attributeCount>0){
                                    person?.id = parser.getAttributeValue(0)
                                }
                            }else{
                                when(this.currentTag){
                                    "name" -> this.person?.name = parser.nextText()
                                    "age" -> this.person?.age = parser.nextText()
                                }
                            }
                        }
                    }
                    XmlPullParser.TEXT ->{
                        //等价于上面
//                        if(!TextUtils.isEmpty(this.currentTag)){
//                            when(this.currentTag){
//                                "name" -> this.person?.name = parser.text
//                                "age" -> this.person?.age = parser.text
//                            }
//                        }
                    }
                    XmlPullParser.END_TAG ->{
                        if (!TextUtils.isEmpty(parser.name) && parser.name==this.nodeName){
                            this.personList.add(this.person as Person)
                            this.person=null
                            this.currentTag=null
                        }
                    }
                    XmlPullParser.END_DOCUMENT ->{}
                }
                eventType = parser.next()
            }
            return personList
        }

    }
}

call test

var inputStream = this.assets.open("persons.xml")
var list = XmlPullParser.parseXmlForPersonList(inputStream,"person")

[ Complete demo ]


References:

1、XML Pull Parsing

2. Parse XML data

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325771718&siteId=291194637