1. 程式人生 > >Java實現地區選擇工具類

Java實現地區選擇工具類

1.需要的jar包

<dependency>
        <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>1.6.1</version>
  </dependency>

2.配置檔案

配置檔案地址(提取碼z4vf)
https://pan.baidu.com/s/1nGZL-QWYZxrBS8LEC0Pgnw

3.工具類

package com.xiaolc.util;
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 LiuJinan
 *
 */
public class LocalUtil {
	//各地區xml檔案路徑
	private static final String LOCAL_LIST_PATH = "config/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"));
		}
	}
	
	/**
	 * 
	 * @author		LiuJinan
	 * @TODO		功能:	獲取所有國家名稱
	 * @time		2016-8-26 上午9:02:05
	 * @return		String[]
	 */
	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> 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<Element>
	 */
	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;
	}
	
	/**
	 * 
	 * @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;
	}
}

4.測試程式碼

package xiaolc;

import com.xiaolc.util.LocalUtil;
import org.junit.Test;

import java.util.List;

/**
 * @author lc
 * @Date: 2018/11/29 11:12
 */
public class test {
    @Test
    public void test1(){
        LocalUtil lu =  LocalUtil.getInstance();
        List<String> list = 	lu.getCities("中國", "北京");
        for(int i=0; i<list.size(); i++){
            System.out.print(list.get(i) + " ");
        }
    }
    @Test
    public void test2(){
        LocalUtil lu =  LocalUtil.getInstance();
        List<String> list = 	lu.getProvinces("中國");
        int j=0;
        for(int i=0; i<list.size(); i++){
            j++;
            System.out.print(list.get(i) + " "+"\n");
        }
        System.out.println(j);
    }
}

5.測試結果

在這裡插入圖片描述
在這裡插入圖片描述