获取中国所有的省市县也可以获取国外的

ps:前端要求咱们后端提供,省市县。

应前端要求,写接口,直接把全国省市县写到数据库?我艹,真有那份心我还写这篇博客干啥!

我的也是借鉴了其他人的信息编写而成的。首先打开QQ存放地址找到这个文件,把这个文件放到自己的项目中,这个是地址文件,我们进行DOM解析,感谢腾讯!没有腾讯就没有我们今天的文章

获取中国所有的省市县也可以获取国外的_第1张图片

 jar包  我是maven 项目


   dom4j
   dom4j
   1.6.1

不废话,上代码

package com.example.demo.UrlUtil;

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;

/**
 * Created by Administrator on 2019/2/21.
 * 选择地区工具,包含全国各地省级市级
 */
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(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"));
        }



    }


    //01获取所有国家名称
    public List getCOUNTRY_REGIONs(){
        List list = new ArrayList<>();
        //1.读取
        reader = new SAXReader();
        //2.获得根元素
        rootElement =  document.getRootElement();
        //3.初始化所有国家名称列表
        Iterator it =  rootElement.elementIterator();
        Element ele = null;
        while (it.hasNext()){
            ele = (Element)it.next();
            list.add(ele.attributeValue("Name"));
        }
       return list;
    }



    public List getCountry(){
        return COUNTRY_REGION;
    }
    /**
     *
     * @author		LiuJinan
     * @TODO		功能:	根据国家名获取该国所有省份
     * @time		2016-8-26 上午9:07:21
     * @param countryName	国家名,从getCountry()从取出
     * @return		List
     */
    private List getprovinces(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;
    }
      //01
    public List provinces(String countryName){
        Iterator it =  rootElement.elementIterator();
        List provinces = new ArrayList();
        Element ele = null;
        while(it.hasNext()){
            ele = (Element)it.next();
            COUNTRY_REGION.add(ele.attributeValue("Name"));
            if(ele.attributeValue("Name").equals(countryName)){
                provinces = ele.elements();
                break;
            }
        }
        return provinces;
    }

    /**
     *
     * @author		LiuJinan
     * @TODO		功能:	根据国家名获取该国所有省份
     * @time		2016-8-26 上午9:07:21
     * @param countryName	国家名,从getCountry()从取出
     * @return		List
     */
    //02获取国家内的省份有哪些
    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 citie(String provinceName ){
        //所有的省
        List provinces =    this.provinces("中国");
        List cities = new ArrayList();
        if(provinces==null || provinces.size()==0){		//没有这个城市

        }
        for (int i = 0;i xian(String citie,String ar){
        //获取到市名称
        List  cit  =this.citie(citie);
        List xian = new ArrayList();
        if(cit ==null ||cit.size() ==0){
            //没有这个城市
        }
        for (int i =0;i getxian(String xian,String ar){
        List tmp = this.xian( xian, ar);
        List xianming = new ArrayList();
        for(int i=0; i getCitie(String provinceName){
        List tmp =  this.citie(provinceName);
        List cities = new ArrayList();
        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

除了文件地址需要更改其他的不用管。直接能用!

//获取中国所有的省份
    @ResponseBody
    @RequestMapping(value = "/region",method = RequestMethod.POST)
     public Object region(HttpServletRequest request){
        // 存放数据
           Map map = new HashMap();
           //
        LocalUtil lu =  LocalUtil.getInstance();
        List sheng = lu.getProvinces("中国");
        //存储中国所有的省份
        map.put("shengfen",sheng);
        //
         return map;
     }
//获取中国所有的市区
    @ResponseBody
    @RequestMapping(value = "/city",method = RequestMethod.POST)
    public Object city(HttpServletRequest request){
        // 存放数据
        Map map = new HashMap();
         String shengfen =request.getParameter("shengfen");
        LocalUtil lu =  LocalUtil.getInstance();
        List  shi = lu.getCitie(shengfen);
        //存储中国所有的省份
        map.put("shi",shi);
        //
        return map;
    }
//获取中国所有的县
    @ResponseBody
    @RequestMapping(value = "/county",method = RequestMethod.POST)
    public Object county(HttpServletRequest request){
        // 存放数据
        Map map = new HashMap();
        String shengfen =request.getParameter("shengfen");
        String shimingcheng = request.getParameter("shimingcheng");
        LocalUtil lu =  LocalUtil.getInstance();
        List  countyName = lu.getxian(shengfen,shimingcheng);
        //存储中国所有的省份
        map.put("countyName",countyName);
        return map;
    }

       

   

 

 

 

你可能感兴趣的:(java)