码迷,mamicode.com
首页 > 编程语言 > 详细

qq QQ 全世界国家和地区代码获取 java

时间:2017-06-09 17:35:58      阅读:318      评论:0      收藏:0      [点我收藏+]

标签:qq 全世界地区 java


java配的工具类足够,下载安装QQ,然后搜索安装目录下LocList.xml文件

以下就是解析这个文件的代码,输出为一堆sql,你懂得,如果要json对象

那修改printNode就可以了


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

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;



public class TestXML {
	
	private static void printNode(Node n, Node parent, int level ) {
		parent = parent==null? n:parent; 
		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 
					+");"
					);
	}
	
	public static void main(String[] args) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document document = db.parse("C:\\tmp\\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);
                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);
                			}
                		}
                	}
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } 
	}
}



本文出自 “Oracle+Linux=>Majesty” 博客,请务必保留此出处http://majesty.blog.51cto.com/3493901/1933673

qq QQ 全世界国家和地区代码获取 java

标签:qq 全世界地区 java

原文地址:http://majesty.blog.51cto.com/3493901/1933673

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!