package com.mzl.flower.utils; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** * 选择地区工具,包含全国各地省级市级 * @author licheng * */ public class LocalUtil { //各地区xml文件路径 private static final String LOCAL_LIST_PATH = "/LocList.xml"; //所有国家名称List private static final List COUNTRY_REGION = new ArrayList(); private static LocalUtil localutil; private SAXReader reader; private Document document; private Element rootElement; //根元素 //初始化 private LocalUtil(){ //1.读取 reader = new SAXReader(); try { document = reader.read(this.getClass().getResourceAsStream(LOCAL_LIST_PATH)); } catch (DocumentException e) { e.printStackTrace(); } //2.获得根元素 rootElement = document.getRootElement(); //3.初始化所有国家名称列表 Iterator it = rootElement.elementIterator(); Element ele = null; while(it.hasNext()){ ele = (Element)it.next(); COUNTRY_REGION.add(ele.attributeValue("Name")); } } /** * * @TODO 功能: 获取所有国家名称 * @return String[] */ public List getCountry(){ return COUNTRY_REGION; } /** * * @TODO 功能: 根据国家名获取该国所有省份 * @param countryName 国家名,从getCountry()从取出 * @return List */ private List provinces(String countryName){ Iterator it = rootElement.elementIterator(); List provinces = new ArrayList(); Element ele = null; while(it.hasNext()){ ele = (Element)it.next(); if(ele.attributeValue("Name").equals(countryName)){ provinces = ele.elements(); break; } } return provinces; } /** * * @TODO 功能: 根据国家名获取该国所有省份 * @param countryName 国家名,从getCountry()从取出 * @return List */ public List getProvinces(String countryName){ List tmp = this.provinces(countryName); List list = new ArrayList(); for(int i=0; i cities(String countryName, String provinceName){ List provinces = this.provinces(countryName); List cities = new ArrayList(); if(provinces==null || provinces.size()==0){ //没有这个城市 return cities; } for(int i=0; i */ public List getCities(String countryName, String provinceName){ List tmp = this.cities(countryName, provinceName); List cities = new ArrayList(); for(int i=0; i districts(String countryName, String provinceName, String city) { //获取城市名称 List cities1 = this.cities(countryName, provinceName); List cities = new ArrayList(); if (cities1 == null || cities1.size() == 0) { //没有这个县 return cities; } for (int i = 0; i < cities1.size(); i++) { if (cities1.get(i).attributeValue("Name").equals(city)) { cities = cities1.get(i).elements(); break; } } return cities; } /** * @param countryName * @param provinceName * @return List * @TODO 功能:根据国家,省份和城市名获取县级名称 */ public List getDistrict(String countryName, String provinceName, String city) { List tmp = this.districts(countryName, provinceName, city); List cities = new ArrayList(); for (int i = 0; i < tmp.size(); i++) { cities.add(tmp.get(i).attributeValue("Name")); } return cities; } public static LocalUtil getInstance(){ if(localutil == null){ localutil = new LocalUtil(); } return localutil; } public static LocalUtil getNewInstance(){ localutil = new LocalUtil(); return localutil; } }