安卓读取assets中的xml文件


 AssetManager asset = context.getAssets();
        try {
           <span style="color:#FF0000;"><strong> InputStream input = asset.open("province_data.xml");
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser parser = spf.newSAXParser();  解析核心代码
            XmlParserHandler handler = new XmlParserHandler();
            parser.parse(input, handler);
            input.close();</strong></span>
          <span style="color:#33CC00;">  provinceList = handler.getDataList();</span>  //只是返回数据
            return provinceList;
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {

        }

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.util.ArrayList;
import java.util.List;

public class XmlParserHandler extends DefaultHandler {


	private List<ProvinceModel> provinceList = new ArrayList<ProvinceModel>();
	 	  
	public XmlParserHandler() {
		
	}

	<span style="color:#009900;">public List<ProvinceModel> getDataList() {
		return provinceList;
	}</span>

	@Override
	public void startDocument() throws SAXException {

	}

	ProvinceModel provinceModel = new ProvinceModel();
	CityModel cityModel = new CityModel();
	DistrictModel districtModel = new DistrictModel();
	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		
		if (qName.equals("province")) {
			provinceModel = new ProvinceModel();
			provinceModel.setName(attributes.getValue(0));
			provinceModel.setCityList(new ArrayList<CityModel>());
		} else if (qName.equals("city")) {
			cityModel = new CityModel();
			cityModel.setName(attributes.getValue(0));
			cityModel.setDistrictList(new ArrayList<DistrictModel>());
		} else if (qName.equals("district")) {
			districtModel = new DistrictModel();
			districtModel.setName(attributes.getValue(0));
			//districtModel.setZipcode(attributes.getValue(1));
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		
		if (qName.equals("district")) {
			cityModel.getDistrictList().add(districtModel);
        } else if (qName.equals("city")) {
        	provinceModel.getCityList().add(cityModel);
        } else if (qName.equals("province")) {
        	provinceList.add(provinceModel);
        }
	}
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
	}

}

<?xml version="1.0" encoding="utf-8"?>
<root>
  <province name="安徽省">
    <city name="安庆市">
      <district name="枞阳县" zipcode="246000" />
      <district name="大观区" zipcode="246000" />
      <district name="桐城市" zipcode="246000" />
      <district name="望江县" zipcode="246000" />
      <district name="宜秀区" zipcode="246000" />
      <district name="迎江区" zipcode="246000" />
      <district name="岳西县" zipcode="246000" />
      <district name="其他" zipcode="246000" />
    </city>
  </province>
</root>


你可能感兴趣的:(安卓读取assets中的xml文件)