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

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

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

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

 jar包  我是maven 项目

<!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
<dependency>
   <groupId>dom4j</groupId>
   <artifactId>dom4j</artifactId>
   <version>1.6.1</version>
</dependency>

不废话,上代码

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<String> COUNTRY_REGION = new ArrayList<String>();
    //本类是地址
    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<String> getCOUNTRY_REGIONs(){
        List<String> 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<String> getCountry(){
        return COUNTRY_REGION;
    }
    /**
     *
     * @author		LiuJinan
     * @TODO		功能:	根据国家名获取该国所有省份
     * @time		2016-8-26 上午9:07:21
     * @param countryName	国家名,从getCountry()从取出
     * @return		List<Element>
     */
    private List<Element> getprovinces(String countryName){
        Iterator it =  rootElement.elementIterator();
        List<Element> provinces = new ArrayList<Element>();

        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<Element> provinces(String countryName){
        Iterator it =  rootElement.elementIterator();
        List<Element> provinces = new ArrayList<Element>();
        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<String>
     */
    //02获取国家内的省份有哪些
    public List<String> getProvinces(String countryName){
        List<Element> tmp = this.provinces(countryName);
        List<String> list = new ArrayList<String>();
        for(int i=0; i<tmp.size(); i++){
            list.add(tmp.get(i).attributeValue("Name"));
        }
        return list;
    }

    /**
     *
     * @author		LiuJinan
     * @TODO		功能:根据国家名和省份名,获取该省城市名列表
     * @time		2016-8-26 上午9:15:24
     * @param
     * @param provinceName
     * @return
     */
    private List<Element> cities(String countryName, String provinceName){
        List<Element> provinces =  this.provinces(countryName);
        List<Element> cities = new ArrayList<Element>();
        if(provinces==null || provinces.size()==0){		//没有这个城市
            return cities;
        }
        for(int i=0; i<provinces.size(); i++){
            if(provinces.get(i).attributeValue("Name").equals(provinceName)){
                cities = provinces.get(i).elements();
                break;
            }
        }
        return cities;
    }
    //获取对应省份的所有市区
    public List<Element> citie(String provinceName ){
        //所有的省
        List<Element> provinces =    this.provinces("中国");
        List<Element> cities = new ArrayList<Element>();
        if(provinces==null || provinces.size()==0){		//没有这个城市

        }
        for (int i = 0;i<provinces.size();i++){

            if(provinces.get(i).attributeValue("Name").equals(provinceName)){
                //这里是所有的省
                cities = provinces.get(i).elements();
                break;
            }
        }
            return cities;
    }
     //获取对应市区的所有市
    public List<Element> xian(String citie,String ar){
        //获取到市名称
        List<Element>  cit  =this.citie(citie);
        List<Element> xian = new ArrayList<Element>();
        if(cit ==null ||cit.size() ==0){
            //没有这个城市
        }
        for (int i =0;i<cit.size();i++){
            if(cit.get(i).attributeValue("Name").equals(ar))
           xian =cit.get(i).elements();
        }
        return  xian;
    }




    //县

    public List<String> getxian(String xian,String ar){
        List<Element> tmp = this.xian( xian, ar);
        List<String> xianming = new ArrayList<String>();
        for(int i=0; i<tmp.size(); i++){
            xianming.add(tmp.get(i).attributeValue("Name"));
        }
        return xianming;
    }


    //市
     public List<String> getCitie(String provinceName){
        List<Element> tmp =  this.citie(provinceName);
        List<String> cities = new ArrayList<String>();
        for(int i=0; i<tmp.size(); i++){
            cities.add(tmp.get(i).attributeValue("Name"));
        }
        return cities;
    }



    /**
     *
     * @author		LiuJinan
     * @TODO		功能:根据国家名和省份名获取城市列表
     * @time		2016-8-26 下午4:55:55
     * @param countryName
     * @param provinceName
     * @return		List<String>
     */
    public List<String> getCities(String countryName, String provinceName){
        List<Element> tmp =  this.cities(countryName, provinceName);
        List<String> cities = new ArrayList<String>();
        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;
    }


}

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

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

       

   

 

 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值