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 countrysModelList = new ArrayList<>(); public XmlParserHandler() { } public List 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()); } 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 { } }