xuxiuxi
2017-03-29 9dbc3d9356d57fecb0f77782838161b53a9852f2
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
package cn.com.basic.face.util;
 
import cn.com.basic.face.model.CountryModel;
import cn.com.basic.face.model.CountrysModel;
 
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * Created by Sinoe on 2017/2/28.
 */
 
public class XmlParserHandler extends DefaultHandler {
    private List<CountrysModel> countrysModelList = new ArrayList<>();
 
    public XmlParserHandler() {
 
    }
    public List<CountrysModel> getDataList() {
        return countrysModelList;
    }
    @Override
    public void startDocument() throws SAXException {
    }
    CountrysModel countrysModel = new CountrysModel();
    CountryModel countryModel = new CountryModel();
 
    @Override
    public void startElement(String uri, String localName, String qName,
                             Attributes attributes) throws SAXException {
        if (qName.equals("word")) {
            countrysModel = new CountrysModel();
            countrysModel.setName(attributes.getValue(0));
            countrysModel.setCountryModelList(new ArrayList<CountryModel>());
        } else if (qName.equals("country")) {
            countryModel = new CountryModel();
            countryModel.setName(attributes.getValue(0));
        }
    }
    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        if (qName.equals("country")) {
            countrysModel.getCountryModelList().add(countryModel);
        } else if (qName.equals("word")) {
            countrysModelList.add(countrysModel);
        }
    }
 
    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
    }
}