import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import com.safetys.customerform.xml.model.Area; /** * * @author hehc * */ public class AreaUtil { public static final String FROM_XML = "xml"; private String from = FROM_XML; private String xmlFileDirectory; private Map<String,Area> areaMap = new HashMap<String, Area>(); // provinceCode, provinceArea private AreaUtil(){ super(); } public final static AreaUtil getInstance(String FROM, String xmlFileDirectory){ AreaUtil areaUtil = new AreaUtil(); areaUtil.setFrom(FROM); areaUtil.setXmlFileDirectory(xmlFileDirectory); areaUtil.init(); return areaUtil; } /** * 解析xml文件 ******************************************************** * template of xml: * * <area name="国家"> * * <first-area name="省" code=""> * * <second-area name="市" code=""> * * <third-area name="区" code=""></third-area> * * </second-area> * * </first-area> * * </area> * ******************************************************** */ private void init(){ if(from != null && from.equals(AreaUtil.FROM_XML)){ DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); if(xmlFileDirectory == null) throw new Exception("xml文件地址为空!"); Document doc = documentBuilder.parse(new FileInputStream(xmlFileDirectory)); Element areaRoot = doc.getDocumentElement(); // System.out.println(areaRoot.getAttribute("name")); NodeList firstAreas = areaRoot.getChildNodes(); Map<String,Area> firstAreaMap = new HashMap<String, Area>(); for(int i = 0; i < firstAreas.getLength(); i++){//遍历first-area标签 Area firstArea = new Area(); Node firstNode = firstAreas.item(i); if(firstNode.getNodeType() == Node.ELEMENT_NODE){ firstArea.setName(firstNode.getAttributes(). getNamedItem("name").getNodeValue()); firstArea.setCode(firstNode.getAttributes(). getNamedItem("code").getNodeValue()); NodeList secondAreas = firstNode.getChildNodes(); Map<String,Area> secondAreaMap = new HashMap<String, Area>(); for(int j = 0; j < secondAreas.getLength(); j++){//遍历second-area标签 Area secondArea = new Area(); Node secondNode = secondAreas.item(j); if(secondNode.getNodeType() == Node.ELEMENT_NODE){ secondArea.setName(secondNode.getAttributes(). getNamedItem("name").getNodeValue()); secondArea.setCode(secondNode.getAttributes(). getNamedItem("code").getNodeValue()); NodeList thirdAreas = secondNode.getChildNodes(); Map<String,Area> thirdAreaMap = new HashMap<String, Area>(); for(int k = 0; k < thirdAreas.getLength(); k++){//遍历third-area标签 Area thirdArea = new Area(); Node thirdNode = thirdAreas.item(k); if(thirdNode.getNodeType() == Node.ELEMENT_NODE){ thirdArea.setName(thirdNode.getAttributes(). getNamedItem("name").getNodeValue()); thirdArea.setCode(thirdNode.getAttributes(). getNamedItem("code").getNodeValue()); thirdAreaMap.put(thirdArea.getCode(), thirdArea); } } secondArea.setAreaMap(thirdAreaMap); secondAreaMap.put(secondArea.getCode(), secondArea); } } firstArea.setAreaMap(secondAreaMap); firstAreaMap.put(firstArea.getCode(), firstArea); } } areaMap = firstAreaMap; } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } /** * 返回当前国家区域的所有下一级(二级)行政单位(中国:省) * @return Map<String provinceCode, String provinceName> */ public Map<String, String> getProvinces(){ Map<String, String> map = new HashMap<String, String>(); if(areaMap != null && !areaMap.isEmpty()){ Iterator<String> it = areaMap.keySet().iterator(); while(it.hasNext()){ String code = it.next(); map.put(code, areaMap.get(code).getName()); } } return map; } /** * 返回当前国家区域、指定二级行政单位下的所有三级行政单位(中国:市) * @param provinceCode * @return Map<String cityCode, String cityName> */ public Map<String, String> getCities(String provinceCode){ Map<String, String> map = new HashMap<String, String>(); if(areaMap != null & !areaMap.isEmpty()){ Area firstArea = areaMap.get(provinceCode); if(firstArea != null){ Map<String, Area> secondAreaMap = firstArea.getAreaMap(); if(secondAreaMap != null && !secondAreaMap.isEmpty()){ Iterator<String> it = secondAreaMap.keySet().iterator(); while(it.hasNext()){ String code = it.next(); map.put(code, secondAreaMap.get(code).getName()); } } } } return map; } /** * 返回当前国家区域、指定二级行政单位下的所有四级行政单位(中国:区) * @param provinceCode * @param cityCode * @return Map<String regionCode, String regionName> */ public Map<String, String> getRegions(String provinceCode, String cityCode){ Map<String, String> map = new HashMap<String, String>(); if(areaMap != null & !areaMap.isEmpty()){ Area firstArea = areaMap.get(provinceCode); if(firstArea != null){ Map<String,Area> secondAreaMap = firstArea.getAreaMap(); if(secondAreaMap != null && !secondAreaMap.isEmpty()){ Area secondArea = secondAreaMap.get(cityCode); if(secondArea != null){ Map<String, Area> thirdAreaMap = secondArea.getAreaMap(); if(thirdAreaMap != null && !thirdAreaMap.isEmpty()){ Iterator<String> it = thirdAreaMap.keySet().iterator(); while(it.hasNext()){ String code = it.next(); map.put(code, thirdAreaMap.get(code).getName()); } } } } } } return map; } /** * 根据当前国家区域、指定某区域代码获取该区域名称 * @param areaCode * @return String */ public String getAreaName(String areaCode) throws Exception{ if(areaMap != null && !areaMap.isEmpty()){ Iterator<String> it = areaMap.keySet().iterator(); while(it.hasNext()){ String firstCode = it.next(); if(firstCode.equals(areaCode)){ return areaMap.get(firstCode).getName(); } Map<String, Area> secondAreaMap = areaMap.get(firstCode).getAreaMap(); if(secondAreaMap != null && !secondAreaMap.isEmpty()){ Iterator<String> it_ = secondAreaMap.keySet().iterator(); while(it_.hasNext()){ String secondCode = it_.next(); if(secondCode.equals(areaCode)){ return secondAreaMap.get(secondCode).getName(); } Map<String, Area> thirdAreaMap = secondAreaMap.get(secondCode).getAreaMap(); if(thirdAreaMap != null && !thirdAreaMap.isEmpty()){ Iterator<String> it__ = thirdAreaMap.keySet().iterator(); while(it__.hasNext()){ String thirdCode = it__.next(); if(thirdCode.equals(areaCode)){ return thirdAreaMap.get(thirdCode).getName(); } } } } } } throw new Exception("未知区域"); } return null; } private void setFrom(String from) { this.from = from; } private void setXmlFileDirectory(String xmlFileDirectory) { this.xmlFileDirectory = xmlFileDirectory; }