qq全球国家地区xml解析

第一种:
package com.cuican.usercenter.utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

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


/**
 * 选择地区工具,包含全国各地省级市级
 * @author LiuJinan
 *
 */
public class LocalUtils {
    //各地区xml文件路径
    private static final String LOCAL_LIST_PATH = "D:\\LocList.xml";
    //所有国家名称List
    private static final List<String> COUNTRY_REGION = new ArrayList<String>();
    private static LocalUtils localutil;
    private SAXReader reader;
    private Document document;
    private Element rootElement;      //根元素

    //初始化
    private LocalUtils(){
        //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    List<String>
     */
    public static 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<String>
     */
    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 province
     * @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 LocalUtils getInstance(){
        if(localutil==null){
            localutil = new LocalUtils();
        }
        return localutil;
    }

    public static void main(String[] args) {
        System.out.println("*******************************************  "+getCountry());
    }

}

 

第二种:

/*
 * ddd.java Copyright BrightStars Tech Co. Ltd. All Rights Reserved.
 */
package com.cuican.usercenter.utils;

import com.cuican.usercenter.entity.SysWorldCountry;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

/**
 * @author: djg
 * @time: 2019/6/3
 **/
public class WorldCountryUtils {


    private static void printNode(Node n, Node parent, int level ) {
        parent = parent==null? n:parent;
        System.out.println("insert into sys_world_country(id,superior_id, pinyin_initials,name, level)values( "
                        + "\""+ n.getAttributes().getNamedItem("Code").getNodeValue()+"\","
                        + "\""+ parent.getAttributes().getNamedItem("Code").getNodeValue() +"\","
                        + "\""+ PinyinDemo.ToPinyin(n.getAttributes().getNamedItem("Name").getNodeValue().substring(0, 1)).substring(0, 1)+"\","
                        + "\""+ n.getAttributes().getNamedItem("Name").getNodeValue()+"\","
                        + "\""+level+"\""
                        +");"

        /*System.out.println("insert into world_country(id,name, pid, level)values( "
                + n.getAttributes().getNamedItem("Code").getNodeValue()+","
                + "\""+ n.getAttributes().getNamedItem("Name").getNodeValue()+"\","
                + "\""+ parent.getAttributes().getNamedItem("Code").getNodeValue() +"\","
                + level
                +");"*/
        );
    }




    //https://blog.csdn.net/heganlin/article/details/52516611

    public static void main(String[] args) throws Exception {
        SysWorldCountry sysWorldCountry = new SysWorldCountry();
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document document = db.parse("D:\\LocList.xml");
            NodeList l = document.getElementsByTagName("CountryRegion");
            System.out.println("一共有" + l.getLength() + "个国家");
            for (int i = 0; i < l.getLength(); i++) {//州
                Node n = l.item(i);
                /*String name = n.getAttributes().getNamedItem("Name").getNodeValue();//国家的名字
                String pinyin = PinyinDemo.ToPinyin(l.item(i)//国家名字拼音首字母
                        .getAttributes().getNamedItem("Name").getNodeValue().substring(0, 1)).substring(0, 1);
                //guojia
                System.out.println("idididididididididididididididididididididid  "+i);
                System.out.println("pinyinpinyinpinyinpinyinpinyinpinyinpinyin  "+pinyin);
                System.out.println("namenamenamenamenamenamenamename  "+name);*/
                printNode(n,null,1);
                NodeList childNodes = n.getChildNodes();
                for (int k = 0; k < childNodes.getLength(); k++) {
                    Node n2 = childNodes.item(k);
                    if("State".equals(n2.getNodeName())){
                        if(n2.getAttributes().getNamedItem("Name")==null){
                            NodeList childNodesS = n2.getChildNodes();
                            for(int z = 0; z < childNodesS.getLength(); z++) {
                                Node ns = childNodesS.item(z);
                                if("City".equals(ns.getNodeName())){
//                              System.out.println("这个国家没有州");
                                    printNode(ns,n,3);
                                }
                            }
                            continue;
                        }
                        printNode(n2,n,2);
                        NodeList childNodes2 = n2.getChildNodes();
                        for(int u = 0; u < childNodes2.getLength(); u++) {
                            Node n3 = childNodes2.item(u);
                            if("City".equals(n3.getNodeName())){
                                printNode(n3,n2,3);
                            }
                        }
                    }
                }
            }
    }
}

 

拼音转汉字:

/*
 * PinyinDemo.java Copyright BrightStars Tech Co. Ltd. All Rights Reserved.
 */
package com.cuican.usercenter.utils;


import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * 汉字转换为拼音
 * @author Red
 */
public class PinyinDemo {
    /**
     * 测试main方法
     * @param args
     */
    public static void main(String[] args) {
        System.out.println(ToFirstChar("汉字转换为拼音").toUpperCase()); //转为首字母大写
        System.out.println(ToPinyin("汉字转换为拼音"));
    }
    /**
     * 获取字符串拼音的第一个字母
     * @param chinese
     * @return
     */
    public static String ToFirstChar(String chinese){
        String pinyinStr = "";
        char[] newChar = chinese.toCharArray();  //转为单个字符
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < newChar.length; i++) {
            if (newChar[i] > 128) {
                try {
                    pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0].charAt(0);
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            }else{
                pinyinStr += newChar[i];
            }
        }
        return pinyinStr;
    }

    /**
     * 汉字转为拼音
     * @param chinese
     * @return
     */
    public static String ToPinyin(String chinese){
        String pinyinStr = "";
        char[] newChar = chinese.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < newChar.length; i++) {
            if (newChar[i] > 128) {
                try {
                    pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0];
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            }else{
                pinyinStr += newChar[i];
            }
        }
        return pinyinStr;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值