最新省市县行政区域(含编码)XML和JSON数据格式(含测试入库代码)

	private Area area;

	int pid1 = 0;
	int pid2 = 0;
	@Autowired
	private IAreaService areaService;




public String insert() throws Exception {

		String path = ServletActionContext.getServletContext().getRealPath("/")
				+ "xmlData/ProvinceAndCity.xml";

		File file = new File(path);
		SAXBuilder sb = new SAXBuilder();
		Document doc = sb.build(file);
		Element rootElement = doc.getRootElement();

		List provinceList = rootElement.getChildren("Province");
		// 省
		for (int i = 0; i < provinceList.size(); i++) {
			area = new Area();
			pid1 = 0;
			Element provinceEle = (Element) provinceList.get(i);
			String provinceId = provinceEle.getAttributeValue("id");
			String provinceName = provinceEle.getAttributeValue("name");

			area.setCode(provinceId);
			area.setName(provinceName);
			area.setPid(pid1);

			this.areaService.insert(area);
			
			pid1 = area.getId();
			
			List cityList = provinceEle.getChildren("City");
			
			
			// 城市
			for (int j = 0; j < cityList.size(); j++) {
				pid2 = 0;
				Element cityEle = (Element) cityList.get(j);
				String cityId = cityEle.getAttributeValue("id");
				String cityName = cityEle.getAttributeValue("name");
				area.setCode(cityId);
				area.setName(cityName);
				area.setPid(pid1);

				this.areaService.insert(area);
				
				pid2 = area.getId();
				
				List areaList = cityEle.getChildren("Area");
				// 区县
				for (int k = 0; k < areaList.size(); k++) {
					Element areaEle = (Element) areaList.get(k);
					String areaId = areaEle.getAttributeValue("id");
					String areaName = areaEle.getAttributeValue("name");

					area.setCode(areaId);
					area.setName(areaName);
					area.setPid(pid2);

					this.areaService.insert(area);

				}
			}
		}

		return null;

	}

 

 

 

你可能感兴趣的:(json)