在 Java 中存储国家/地区代码、名称和大陆的最佳方式 [英] Best way to store Country codes, names, and Continent in Java

查看:41
本文介绍了在 Java 中存储国家/地区代码、名称和大陆的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个 ListArray 某种类型的,存储每个国家的信息:

I want to have a List or Array of some sort, storing this information about each country:

  • 2 个字母代码
  • 巴西等国名
  • 世界大陆/地区,例如东欧、北美等.

我会手动将每个国家/地区分类到区域/大陆(但如果有自动执行此操作的方法,请告诉我).这个问题是关于如何存储和访问国家/地区.例如,我希望能够检索北美的所有国家/地区.

I will classify each country into the region/continent manually (but if there exists a way to do this automatically, do let me know). This question is about how to store and access the countries. For example, I want to be able to retrieve all the countries in North America.

我不想使用本地文本文件等,因为该项目将使用 Google Web Toolkit 转换为 javascript.但我真正想要的是将其存储在 Enum 或其他某种资源文件中,使其与其余代码分开.

I don't want to use local text files or such because this project will be converted to javascript using Google Web Toolkit. But storing in an Enum or another resource file of some sort, keeping it separate from the rest of the code, is what I'm really after.

推荐答案

ISO 3166 中有 246 个国家/地区,您可能会在此基础上获得一个中继大枚举.我更喜欢使用带有国家/地区列表的 XML 文件,您可以从 http://下载一个www.iso.org/ 并加载它们(例如当应用程序启动时).然后,当您在 GWT 中需要它们时,将它们作为 RPC 调用加载回来,但请记住缓存它们(某种延迟加载),这样您就不会每次都完成加载它们.我认为这总比将它们保存在代码中要好,因为每次访问模块时,您都会完成加载完整列表,即使用户不需要使用此列表.

There is 246 countries in ISO 3166, you might get a relay big enum on back of this. I prefer to use XML file with list of countries, you can download one from http://www.iso.org/ and load them (e.g. when app is starting). Than, as you need them in GWT load them in back as RPC call, but remember to cache those (some kind of lazy loading) so you wont finish with loading them each time. I think this would be anyway better than holding them in code, as you will finish with loading full list each time module is accessed, even if user will not need to use this list.

所以你需要一些可以保存国家的东西:

So you need something which will hold country:

public class Country
{
    private final String name;
    private final String code;

    public Country(String name, String code)
    {
        this.name = name;
        this.code = code;
    }

    public String getName()
    {
        return name;
    }

    public String getCode()
    {
        return code;
    }

    public boolean equals(Object obj)
    {
        if (this == obj)
        {
            return true;
        }
        if (obj == null || getClass() != obj.getClass())
        {
            return false;
        }

        Country country = (Country) obj;

        return code.equals(country.code);
    }

    public int hashCode()
    {
        return code.hashCode();
    }
}

对于 GWT,这个类需要实现 IsSerializable.你可以在服务器端使用:

For GWT this class would need to implement IsSerializable. And you can load those, on server side using:

import java.util.ArrayList;
import java.util.List;
import java.io.InputStream;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class CountriesService
{
    private static final String EL_COUNTRY = "ISO_3166-1_Entry";
    private static final String EL_COUNTRY_NAME = "ISO_3166-1_Country_name";
    private static final String EL_COUNTRY_CODE = "ISO_3166-1_Alpha-2_Code_element";
    private List<Country> countries = new ArrayList<Country>();

    public CountriesService(InputStream countriesList)
    {
        parseCountriesList(countriesList);
    }

    public List<Country> getCountries()
    {
        return countries;
    }

    private void parseCountriesList(InputStream countriesList)
    {
        countries.clear();
        try
        {
            Document document = parse(countriesList);
            Element root = document.getRootElement();
            //noinspection unchecked
            Iterator<Element> i = root.elementIterator(EL_COUNTRY);
            while (i.hasNext())
            {
                Element countryElement = i.next();
                Element countryName = countryElement.element(EL_COUNTRY_NAME);
                Element countryCode = countryElement.element(EL_COUNTRY_CODE);

                String countryname = countryName.getText();
                countries.add(new Country(countryname, countryCode.getText()));
            }
        }
        catch (DocumentException e)
        {
            log.error(e, "Cannot read countries list");
        }
        catch (IOException e)
        {
            log.error(e, "Cannot read countries list");
        }
    }

    public static Document parse(InputStream inputStream) throws DocumentException
    {
        SAXReader reader = new SAXReader();
        return reader.read(inputStream);
    }
}

当然,如果您需要通过 ISO 2 字母代码查找国家/地区,您可能不会将 List 更改为 Map.如果,正如您所提到的,您需要按大陆划分不同的国家/地区,您可以从 ISO 3166 扩展 XML 并添加您自己的元素.只需查看他们的(ISO 网站)许可证即可.

Of course, if you need to find country by ISO 2 letter code you might wont to change List to Map probably. If, as you mentioned, you need separate countries by continent, you might extend XML from ISO 3166 and add your own elements. Just check their (ISO website) license.

这篇关于在 Java 中存储国家/地区代码、名称和大陆的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆