运行前先导入dom4j架包,由于我们公司用的代理服务器所以下面我设置了代理ip,不需要的可直接忽略
package com.chengshidaima.tools;import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;public class CityCode
{// private Logger log = Logger.getLogger(CityCode.class.getName());public static void main(String[] args) {try {CityCode cc = new CityCode();cc.getCityXml();//System.out.println(doc.asXML());} catch (Exception e) {e.printStackTrace();}}/*** 解析* * @return* @throws Exception*/@SuppressWarnings("unchecked")private Document getCityXml() throws Exception {/** 新建一个doc 保存结果 */Document docResult = DocumentHelper.createDocument();Element addElementRoot = DocumentHelper.createElement("china");String sUrlChina = "http://flash.weather.com.cn/wmaps/xml/china.xml";String sChinaProvXml = getUrlString(sUrlChina);Document doc = DocumentHelper.parseText(sChinaProvXml);Element rootElt = doc.getRootElement(); // 获取根节点//log.info("根节点:" + rootElt.getName()); // 拿到根节点的名称/** 取得所有省份 */List listProv = rootElt.elements("city"); // 获取根节点下的子节点for (int i = 0; i < listProv.size(); i++) {Element elementProv = listProv.get(i);/** 省份名字 */String sProvName = elementProv.attributeValue("pyName");String sProvNameCN = elementProv.attributeValue("quName");/** 添加节点 */Element addElementProv = DocumentHelper.createElement("prov");addElementProv.addAttribute("pyName", sProvName);addElementProv.addAttribute("quName", sProvNameCN);try {//log.info("查询省份:" + sProvNameCN + " 下的城市!");String sUrlProv = "http://flash.weather.com.cn/wmaps/xml/" + sProvName + ".xml";String sProvXml = getUrlString(sUrlProv);Document docProv = DocumentHelper.parseText(sProvXml);Element rootEltProv = docProv.getRootElement(); // 获取省份下的跟节点List listCity1 = rootEltProv.elements("city");for (int j = 0; j < listCity1.size(); j++) {Element elementCity1 = listCity1.get(j);String sCityName1 = elementCity1.attributeValue("pyName");String sCityUrl1 = elementCity1.attributeValue("url");String sCityNameCN1 = elementCity1.attributeValue("cityname");System.out.println(sCityNameCN1 + ":" + sCityUrl1);Element addElementMainCity = DocumentHelper.createElement("city");addElementMainCity.addAttribute("pyName", sCityName1);addElementMainCity.addAttribute("quName", sCityNameCN1);addElementMainCity.addAttribute("url", sCityUrl1);try {String sUrlCity1 = "http://flash.weather.com.cn/wmaps/xml/" + sCityName1 + ".xml";//log.info("查询主要城市:" + sCityNameCN1 + " 下的城市!");String sCityXml = getUrlString(sUrlCity1);Document docCity1 = DocumentHelper.parseText(sCityXml);Element rootCity1 = docCity1.getRootElement();List listCity2 = rootCity1.elements("city");for (int k = 0; k < listCity2.size(); k++) {Element elementCity2 = listCity2.get(k);String sCityName2 = elementCity2.attributeValue("pyName");String sCityNameCN2 = elementCity2.attributeValue("cityname");String sCityUrl2 = elementCity2.attributeValue("url");if (!sCityUrl1.endsWith(sCityUrl2)) {System.out.println(sCityNameCN2 + ":" + sCityUrl2);}Element addElementCity2 = DocumentHelper.createElement("city2");addElementCity2.addAttribute("pyName", sCityName2);addElementCity2.addAttribute("quName", sCityNameCN2);addElementCity2.addAttribute("url", sCityUrl2);addElementMainCity.add(addElementCity2);}} catch (Exception e) {// log.info("查询主要城市:" + sCityNameCN1 + " 下的城市失败!");}addElementProv.add(addElementMainCity);}} catch (Exception e) {//log.info("查询省份:" + sProvNameCN + "下的城市失败!");}addElementRoot.add(addElementProv);}docResult.setRootElement(addElementRoot);return docResult;}/*** 从url获取xml字符串* * @param sUrl* @return* @throws Exception*/private String getUrlString(String sUrl) throws Exception {String sResult = "";StringBuffer sbResult = null;// log.info("开始连接Url:" + sUrl);sbResult = new StringBuffer();URL url = new URL(sUrl);// 创建代理服务器 InetSocketAddress addr = new InetSocketAddress("10.128.6.3",808); Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); // http 代理 URLConnection con = url.openConnection(proxy);BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));String sLine = null;while ((sLine = br.readLine()) != null) {sbResult.append(sLine);}sResult = sbResult.toString();//log.info("获取到信息:" + sbResult.toString().substring(0, 500));if (sResult.contains("html")) {String sInt = "null";Integer.parseInt(sInt);}return sResult;}
}
运行部分结果如下图: