18034727885
2026-08-25 d081165686e520cc06cb87f928ed70d32401c138
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package uni.dcloud.io.uniplugin_richalert.Info;
 
import android.text.TextUtils;
 
import com.alibaba.fastjson.JSONObject;
 
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
 
import java.util.ArrayList;
 
 
public class SaxHelper extends DefaultHandler {
    private Person person;
    private ArrayList<Person> persons;
    //当前解析的元素标签
    private String tagName = null;
 
    /**
     * 当读取到文档开始标志是触发,通常在这里完成一些初始化操作
     */
    @Override
    public void startDocument() throws SAXException {
        this.persons = new ArrayList<Person>();
        //Log.i("SAX", "读取到文档头,开始解析xml");
    }
 
 
    /**
     * 读到一个开始标签时调用,第二个参数为标签名,最后一个参数为属性数组
     */
    @Override
    public void startElement(String uri, String localName, String qName,
                             Attributes attributes) throws SAXException {
        this.person = new Person();
        this.persons.add(this.person);
        person.label = localName;
        if(attributes != null && attributes.getLength() > 0) {
            JSONObject attr = new JSONObject();
            for(int i=0;i<attributes.getLength();i++) {
                attr.put(attributes.getLocalName(i), attributes.getValue(i));
            }
            person.attribute = attr;
        }
        this.tagName = localName;
    }
 
    /**
     * 读到到内容,第一个参数为字符串内容,后面依次为起始位置与长度
     */
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        //判断当前标签是否有效
        String data = new String(ch, start, length);
        if (this.tagName != null) {
            if(TextUtils.isEmpty(this.person.content)) {
                this.person.content = data;
            } else {
                this.person.content = this.person.content + data;
            }
        } else {
            //补充遗漏text
            this.person = new Person();
            this.persons.add(this.person);
            person.label = "text";
            person.content = data;
            person = null;
        }
 
    }
 
    /**
     * 处理元素结束时触发,这里将对象添加到结合中
     */
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        this.person = null;
        //Log.i("SAX", "处理person元素结束~");
        this.tagName = null;
    }
 
    /**
     * 读取到文档结尾时触发,
     */
    @Override
    public void endDocument() throws SAXException {
        super.endDocument();
        //Log.i("SAX", "读取到文档尾,xml解析结束");
    }
 
    //获取persons集合
    public ArrayList<Person> getPersons() {
        return persons;
    }
 
}