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();
}
}
}
转载于:https://blog.51cto.com/majesty/1933673