利用dom4j包,与jaxen包来完成xml解析。需要先下载dom4j包,jaxen包。
xml例子
<?xml version="1.0" encoding="UTF-8"?>
<root Name="中国" ID="86">
<node Name="北京市" ChildCount="2" ID="110000">
<node Name="市辖区" ChildCount="16" ID="110100">
<node Name="东城区" ChildCount="0" ID="110101"/>
<node Name="西城区" ChildCount="0" ID="110102"/>
<node Name="崇文区" ChildCount="0" ID="110103"/>
<node Name="宣武区" ChildCount="0" ID="110104"/>
<node Name="朝阳区" ChildCount="0" ID="110105"/>
<node Name="丰台区" ChildCount="0" ID="110106"/>
<node Name="石景山区" ChildCount="0" ID="110107"/>
<node Name="海淀区" ChildCount="0" ID="110108"/>
<node Name="门头沟区" ChildCount="0" ID="110109"/>
<node Name="房山区" ChildCount="0" ID="110111"/>
<node Name="通州区" ChildCount="0" ID="110112"/>
<node Name="顺义区" ChildCount="0" ID="110113"/>
<node Name="昌平区" ChildCount="0" ID="110114"/>
<node Name="大兴区" ChildCount="0" ID="110115"/>
<node Name="怀柔区" ChildCount="0" ID="110116"/>
<node Name="平谷区" ChildCount="0" ID="110117"/>
</node>
<node Name="县" ChildCount="2" ID="110200">
<node Name="密云县" ChildCount="0" ID="110228"/>
<node Name="延庆县" ChildCount="0" ID="110229"/>
</node>
</node>
<node Name="陕西省" ChildCount="10" ID="610000">
<node Name="西安市" ChildCount="13" ID="610100">
<node Name="新城区" ChildCount="0" ID="610102"/>
<node Name="碑林区" ChildCount="0" ID="610103"/>
<node Name="莲湖区" ChildCount="0" ID="610104"/>
<node Name="灞桥区" ChildCount="0" ID="610111"/>
<node Name="未央区" ChildCount="0" ID="610112"/>
<node Name="雁塔区" ChildCount="0" ID="610113"/>
<node Name="阎良区" ChildCount="0" ID="610114"/>
<node Name="临潼区" ChildCount="0" ID="610115"/>
<node Name="长安区" ChildCount="0" ID="610116"/>
<node Name="蓝田县" ChildCount="0" ID="610122"/>
<node Name="周至县" ChildCount="0" ID="610124"/>
<node Name="户县" ChildCount="0" ID="610125"/>
<node Name="高陵县" ChildCount="0" ID="610126"/>
</node>
<node Name="铜川市" ChildCount="4" ID="610200">
<node Name="王益区" ChildCount="0" ID="610202"/>
<node Name="印台区" ChildCount="0" ID="610203"/>
<node Name="耀州区" ChildCount="0" ID="610204"/>
<node Name="宜君县" ChildCount="0" ID="610222"/>
</node>
<node Name="咸阳市" ChildCount="14" ID="610400">
<node Name="秦都区" ChildCount="0" ID="610402"/>
<node Name="杨凌区" ChildCount="0" ID="610403"/>
<node Name="渭城区" ChildCount="0" ID="610404"/>
<node Name="三原县" ChildCount="0" ID="610422"/>
<node Name="泾阳县" ChildCount="0" ID="610423"/>
<node Name="乾县" ChildCount="0" ID="610424"/>
<node Name="礼泉县" ChildCount="0" ID="610425"/>
<node Name="永寿县" ChildCount="0" ID="610426"/>
<node Name="彬县" ChildCount="0" ID="610427"/>
<node Name="长武县" ChildCount="0" ID="610428"/>
<node Name="旬邑县" ChildCount="0" ID="610429"/>
<node Name="淳化县" ChildCount="0" ID="610430"/>
<node Name="武功县" ChildCount="0" ID="610431"/>
<node Name="兴平市" ChildCount="0" ID="610481"/>
</node>
<node Name="渭南市" ChildCount="11" ID="610500">
<node Name="临渭区" ChildCount="0" ID="610502"/>
<node Name="华县" ChildCount="0" ID="610521"/>
<node Name="潼关县" ChildCount="0" ID="610522"/>
<node Name="大荔县" ChildCount="0" ID="610523"/>
<node Name="合阳县" ChildCount="0" ID="610524"/>
<node Name="澄城县" ChildCount="0" ID="610525"/>
<node Name="蒲城县" ChildCount="0" ID="610526"/>
<node Name="白水县" ChildCount="0" ID="610527"/>
<node Name="富平县" ChildCount="0" ID="610528"/>
<node Name="韩城市" ChildCount="0" ID="610581"/>
<node Name="华阴市" ChildCount="0" ID="610582"/>
</node>
<node Name="汉中市" ChildCount="11" ID="610700">
<node Name="汉台区" ChildCount="0" ID="610702"/>
<node Name="南郑县" ChildCount="0" ID="610721"/>
<node Name="城固县" ChildCount="0" ID="610722"/>
<node Name="洋县" ChildCount="0" ID="610723"/>
<node Name="西乡县" ChildCount="0" ID="610724"/>
<node Name="勉县" ChildCount="0" ID="610725"/>
<node Name="宁强县" ChildCount="0" ID="610726"/>
<node Name="略阳县" ChildCount="0" ID="610727"/>
<node Name="镇巴县" ChildCount="0" ID="610728"/>
<node Name="留坝县" ChildCount="0" ID="610729"/>
<node Name="佛坪县" ChildCount="0" ID="610730"/>
</node>
<node Name="商洛市" ChildCount="7" ID="611000">
<node Name="商州区" ChildCount="0" ID="611002"/>
<node Name="洛南县" ChildCount="0" ID="611021"/>
<node Name="丹凤县" ChildCount="0" ID="611022"/>
<node Name="商南县" ChildCount="0" ID="611023"/>
<node Name="山阳县" ChildCount="0" ID="611024"/>
<node Name="镇安县" ChildCount="0" ID="611025"/>
<node Name="柞水县" ChildCount="0" ID="611026"/>
</node>
</node>
</root>
//创建SaxReader对象
SAXReader saxReader = new SAXReader();
InputStream inputStream = null;
//如果是android studio,则 inputStream=context.getClass().getClassLoader().getResourceAsStream("assets/china_city.xml");//assets文件夹在
与java,res文件夹并列的位置//如果是eclipse,则inputStream = context.getResources().getAssets().open("china_city.xml");//xml文件在assets文件夹下,assets在res与src文件夹并列的位置
//读取xml文件,获得Document对象
Document document = saxReader.read(inputStream);然后调用Document对象的selectnode(String Xpath)方法来定位List list=document.selectNodes("root/node");//返回XMLNodes集合,参数为有效的 XPath 字符串Iterator iterator=list.iterator();
即可遍历出所有省,同理可获得某个省对应的所有市,获得某个市对应的所有区while (iterator.hasNext()){ Element element= (Element) iterator.next(); String provinceName=element.attributeValue("Name"); String provinceId=element.attributeValue("ID"); Log.i("省:",provinceName); Log.i("省区域ID:",provinceId); // } }//获取某一个省下面的市 List list1 = document.selectNodes("/root/node[@ID='410000']/node");//获取河南省的所有市节点集合 Iterator iter=list1.iterator(); while (iter.hasNext()){ Element element= (Element) iter.next(); String henan_city=element.attributeValue("Name");//获取属性值 String henan_city_id=element.attributeValue("ID"); Log.i("henan_city:",henan_city); Log.i("henan_city_id:",henan_city_id);//获取区,有多少个节点,就可以往下无限获取,与节点名无关。解析不同节点名的xml文件时过程相同。 Iterator iterator2=element.elementIterator(); while (iterator2.hasNext()){ Element element2= (Element) iterator2.next(); String areaName=element2.attributeValue("Name"); String areaId=element2.attributeValue("ID"); Log.i("区:",areaName); Log.i("区域ID:",areaId); } }如果是节点不同的大文件用sax解析响应速度快,dom4j解析不适合大文件解析,响应速度大概有一两秒反应时间,小文件dom4j解析响应速度不影响,且操作更简便。
demo下载地址:https://github.com/frontin/AnalyticXml