[数据][xml格式] 2012年统计用区划代码和城乡划分代码

前端之家收集整理的这篇文章主要介绍了[数据][xml格式] 2012年统计用区划代码和城乡划分代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

很早之前发过一个sql的行政区划代码,是到县一级的,这次的代码是到村一级的。

中国行政区划代码数据库文件 - 最新县及县以上行政区划代码(截止2012年10月31日)

http://www.jb51.cc/article/p-wwujmdhh-hs.html


2012年统计用区划代码和城乡划分代码

数据来源:http://www.stats.gov.cn/zjtj/tjbz/tjyqhdmhcxhfdm/2012/

格式:XML

预览图:





下载地址:

全国数据(所有数据都在一个XML内):http://pan.baidu.com/s/1hqxD6vU

分省数据:http://pan.baidu.com/s/1c0nCrhm

全国+分省数据:http://pan.baidu.com/s/1o6yNmZK


如果想自己获取,可以运行下面文件(源码在最下面):

Jar文件:http://pan.baidu.com/s/1i30trNJ


XML使用方法:用你会的方法读取XML即可,总比网页获取方便。或者看下面的源码,使用xstream直接读取。

JAR使用方法:如java -jar iseaSpider.jar d:/folderA/ 行政区划_ALL.xml

程序不自动创建目录,请保证目录存在。

程序代码

一共三个类,其中有两个类代码比较少,直接上截图:


枚举对象:



节点对象,setter和getter省略



依赖JAR包:


技术说明:使用jsoup抓取数据,使用xstream读写xml,具体看代码

  1. package com.isea.aab301;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.nio.charset.Charset;
  9. import java.util.ArrayList;
  10. import java.util.Iterator;
  11. import java.util.List;
  12.  
  13. import org.jsoup.Jsoup;
  14. import org.jsoup.nodes.Document;
  15. import org.jsoup.nodes.Element;
  16. import org.jsoup.select.Elements;
  17.  
  18. import com.thoughtworks.xstream.XStream;
  19.  
  20. /**
  21. * 获取行政区划信息,写入到xml文件
  22. * @author liuzh
  23. *
  24. */
  25. public class Spider {
  26. public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/31.0.1650.63 Safari/537.36";
  27.  
  28. private static final String BASE_URL = "http://www.stats.gov.cn/zjtj/tjbz/tjyqhdmhcxhfdm/2012/";
  29. private static final String INDEX = "index.html";
  30. private int total = 0;
  31. private String filePath;
  32. public void setFilePath(final String filePath) {
  33. String path = filePath.replaceAll("\\\\","/");
  34. if(!path.endsWith("/")){
  35. path+="/";
  36. }
  37. this.filePath = path;
  38. }
  39.  
  40. /**
  41. * @param args
  42. */
  43. public static void main(String[] args) {
  44. if(args.length<1||args.length>2){
  45. System.out.println("参数错误");
  46. System.out.println("程序需要一个保存XML的路径和一个可选参数xml文件名,xml文件名默认为\"行政区划_ALL.xml\"");
  47. System.out.println("如java -jar iseaSpider.jar d:/folderA/ 行政区划_ALL.xml");
  48. System.out.println("如果字符串有空格,请使用双引号\"");
  49. return;
  50. }
  51. Spider spider = new Spider();
  52. String fileName = "行政区划_ALL.xml";
  53. if(args.length==2){
  54. fileName = args[1];
  55. }
  56. spider.setFilePath(args[0]);
  57. List<Isea> iseaList = spider.getALL();
  58. //写入到xml
  59. try {
  60. //spider.toXml(iseaList,"d:/a.xml");
  61. spider.toXml(iseaList,fileName);
  62. } catch (Exception e) {
  63. e.printStackTrace();
  64. }
  65. }
  66. /**
  67. * 写入到xml
  68. * @param list
  69. * @param filePath
  70. * @throws Exception
  71. */
  72. public void toXml(List<Isea> list,String fileName) throws Exception{
  73. XStream xstream = new XStream();
  74. xstream.alias("areas",List.class);
  75. xstream.autodetectAnnotations(true);
  76. FileOutputStream fos = null;
  77. try {
  78. fos = new FileOutputStream(new File(filePath+fileName));
  79. fos.write("<?xml version=\"1.0\" encoding=\"GBK\" ?>\n".getBytes());
  80. xstream.toXML(list,fos);
  81. } finally {
  82. try {
  83. if(fos!=null){
  84. fos.close();
  85. }
  86. } catch (Exception e) {
  87. throw new Exception(e.getMessage());
  88. }
  89. }
  90. }
  91. /**
  92. * 写入到xml
  93. * @param list
  94. * @param filePath
  95. * @throws Exception
  96. */
  97. @SuppressWarnings("unchecked")
  98. public List<Isea> fromXml(String fileName) {
  99. System.out.println("读取:"+fileName);
  100. XStream xstream = new XStream();
  101. xstream.alias("areas",ArrayList.class);
  102. xstream.alias("area",Isea.class);
  103. xstream.autodetectAnnotations(true);
  104. FileInputStream fis = null;
  105. try {
  106. fis = new FileInputStream(new File(filePath+fileName));
  107. return (List<Isea>)xstream.fromXML(new File(filePath+fileName),Charset.forName("UTF8"));
  108. } catch (FileNotFoundException e) {
  109. e.printStackTrace();
  110. } finally {
  111. if(fis!=null){
  112. try {
  113. fis.close();
  114. } catch (IOException e) {
  115. }
  116. }
  117. }
  118. return null;
  119. }
  120. /**
  121. * 判断文件是否存在
  122. * @param fileName
  123. * @return
  124. */
  125. private boolean fileExists(String fileName){
  126. File file = new File(filePath+fileName);
  127. if(file.exists()){
  128. return true;
  129. }
  130. return false;
  131. }
  132. /**
  133. * 获取全部级别信息
  134. * @return
  135. */
  136. public List<Isea> getALL(){
  137. Document doc = getDoc(BASE_URL+INDEX);
  138. List<Isea> iseaList = getProvince(doc);
  139. for(Isea isea:iseaList){
  140. if(fileExists(isea.getText()+".xml")){
  141. //已经存在..直接读取
  142. isea.setChildren(fromXml(isea.getText()+".xml"));
  143. }
  144. else {
  145. isea.setChildren(getCity(isea));
  146. //单个文件写入
  147. try {
  148. toXml(isea.getChildren(),isea.getText()+".xml");
  149. } catch (Exception e) {
  150. System.out.println(e.getMessage());
  151. }
  152. }
  153. }
  154. return iseaList;
  155. }
  156. /**
  157. * 获取省-直辖市地址
  158. * @param baseDoc
  159. * @return
  160. */
  161. public List<Isea> getProvince(Document baseDoc){
  162. Elements elements = baseDoc.select(".provincetr td a");
  163. Iterator<Element> iter = elements.iterator();
  164. Element element = null;
  165. List<Isea> list = new ArrayList<Isea>();
  166. Isea a = null;
  167. while (iter.hasNext()) {
  168. element = iter.next();
  169. a = new Isea();
  170. String href = element.attr("href");
  171. a.setType(DM.PROVINCE.toString());
  172. a.setLevel(1);
  173. a.setHref(BASE_URL+href);
  174. a.setText(element.text());
  175. a.setCode(href.substring(0,href.lastIndexOf(".")));
  176. System.out.println("省:"+a.getText() + " - 代码:"+a.getCode());
  177. list.add(a);
  178. }
  179. return list;
  180. }
  181.  
  182. /**
  183. * 获取
  184. * @param baseDoc
  185. * @return
  186. */
  187. public List<Isea> getCity(Isea isea){
  188. if(isea.getHref()==null||isea.getHref().equals("")){
  189. return null;
  190. }
  191. String baseURL = isea.getHref();
  192. baseURL = baseURL.substring(0,baseURL.lastIndexOf("/")+1);
  193. return getBase(isea,DM.CITY,baseURL);
  194. }
  195. /**
  196. * 获取县区
  197. * @param baseDoc
  198. * @return
  199. */
  200. public List<Isea> getCounty(Isea isea){
  201. //http://www.stats.gov.cn/zjtj/tjbz/tjyqhdmhcxhfdm/2012/13/ 1301.html
  202. if(isea.getHref()==null||isea.getHref().equals("")){
  203. return null;
  204. }
  205. String baseURL = isea.getHref();
  206. baseURL = baseURL.substring(0,DM.COUNTY,baseURL);
  207. }
  208. /**
  209. * 获取乡镇
  210. * @param baseDoc
  211. * @return
  212. */
  213. public List<Isea> getTown(Isea isea){
  214. //http://www.stats.gov.cn/zjtj/tjbz/tjyqhdmhcxhfdm/2012/13/01/ 130102.html
  215. //http://www.stats.gov.cn/zjtj/tjbz/tjyqhdmhcxhfdm/2012/13/01/02/ 130102001.html
  216. if(isea.getHref()==null||isea.getHref().equals("")){
  217. return null;
  218. }
  219. String baseURL = isea.getHref();
  220. baseURL = baseURL.substring(0,DM.TOWN,baseURL);
  221. }
  222. /**
  223. * 获取村社区
  224. * @param baseDoc
  225. * @return
  226. */
  227. public List<Isea> getvillage(Isea isea){
  228. return getBase(isea,DM.VILLAGE,null);
  229. }
  230. /**
  231. * 获取基础
  232. * @param baseDoc
  233. * @return
  234. */
  235. public List<Isea> getBase(Isea isea,DM dm,String baseURL){
  236. if(isea.getHref()==null){
  237. return null;
  238. }
  239. Document doc = getDoc(isea.getHref());
  240. if(doc==null){
  241. return null;
  242. }
  243. Elements elements = doc.select(dm.val());
  244. Iterator<Element> iter = elements.iterator();
  245. Element element = null;
  246. List<Isea> list = new ArrayList<Isea>();
  247. Isea a = null;
  248. while (iter.hasNext()) {
  249. element = iter.next();
  250. Elements es = null;
  251. es = element.select("td");
  252. Iterator<Element> is = es.iterator();
  253. a = new Isea();
  254. //设置类型 - 村一级的特殊
  255. a.setType(dm.toString());
  256. //等级
  257. a.setLevel(isea.getLevel()+1);
  258. Element el = null;
  259. if(is.hasNext()){
  260. el = is.next();
  261. if(el.select("a").size()>0){
  262. String href = el.select("a").first().attr("href");
  263. if(href!=null&&!href.equals("")&&baseURL!=null){
  264. a.setHref(baseURL+href);
  265. } else {
  266. a.setHref(null);
  267. }
  268. } else {
  269. a.setHref(null);
  270. }
  271. a.setCode(el.text());
  272. }
  273. //VILLAGE特殊
  274. if(dm ==DM.VILLAGE){
  275. if(is.hasNext()){
  276. el = is.next();
  277. a.setType(el.text());
  278. }
  279. }
  280. if(is.hasNext()){
  281. //从这个上面也能获取URL,如果URL不存在,则将之前的URL = null
  282. el = is.next();
  283. if(el.select("a").size()>0){
  284. String href = el.select("a").first().attr("href");
  285. if(href!=null&&!href.equals("")&&baseURL!=null){
  286. a.setHref(baseURL+href);
  287. } else {
  288. a.setHref(null);
  289. }
  290. } else {
  291. a.setHref(null);
  292. }
  293. a.setText(el.text());
  294. }
  295. System.out.println("序号:"+(total++)+" - 名称:"+a.getText() + " - 代码:"+a.getCode());
  296. //获取子节点
  297. switch (dm) {
  298. case CITY:
  299. //获取子节点
  300. a.setChildren(getCounty(a));
  301. break;
  302. case COUNTY:
  303. a.setChildren(getTown(a));
  304. break;
  305. case TOWN:
  306. a.setChildren(getvillage(a));
  307. break;
  308. default:
  309. break;
  310. }
  311. list.add(a);
  312. }
  313. return list;
  314. }
  315. /**
  316. * 获取网页内容
  317. * @param url
  318. * @return
  319. */
  320. public Document getDoc(String url){
  321. Document doc = null;
  322. try {
  323. doc = Jsoup.connect(url).timeout(0).userAgent(USER_AGENT).get();
  324. } catch (Exception e) {
  325. System.out.println(e.getMessage());
  326. }
  327. return doc;
  328. }
  329.  
  330. }

主要代码很简单,上面是为了处理一些特殊情况,代码增加不少。。

为了防止获取过程中断线等异常情况,程序会分省写入xml,再次执行的时候会直接读取已有的xml,不会从头开始。


使用上面XML的时候,上面源码中提供了方法

  1. /**
  2. * 写入到xml
  3. * @param list
  4. * @param filePath
  5. * @throws Exception
  6. */
  7. @SuppressWarnings("unchecked")
  8. public List<Isea> fromXml(String fileName) {
  9. System.out.println("读取:"+fileName);
  10. XStream xstream = new XStream();
  11. xstream.alias("areas",Charset.forName("UTF8"));
  12. } catch (FileNotFoundException e) {
  13. e.printStackTrace();
  14. } finally {
  15. if(fis!=null){
  16. try {
  17. fis.close();
  18. } catch (IOException e) {
  19. }
  20. }
  21. }
  22. return null;
  23. }


转载或分享数据请注明作者。

猜你在找的XML相关文章