第一种: 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 ListCOUNTRY_REGION = new ArrayList (); 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 */ public static List getCountry(){ return COUNTRY_REGION; } /** * * @author LiuJinan * @TODO 功能: 根据国家名获取该国所有省份 * @time 2016-8-26 上午9:07:21 * @param countryName 国家名,从getCountry()从取出 * @return List */ private 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 */ 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 */ public List getCities(String countryName, String provinceName){ List tmp = this.cities(countryName, provinceName); List cities = new ArrayList (); for(int i=0; i
第二种:
/* * 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; } }