标签:des style blog http java 使用
java利用dom4j解析xml 需要的jar包:
dom4j官方网站在 http://www.dom4j.org/
下载dom4j-1.6.1.zip
解开后有两个包,仅操作XML文档的话把dom4j-1.6.1.jar加入工程就可以了,如果需要使用XPath的话还需要加入包jaxen-1.1-beta-7.jar(如果不导入这个架包程序可以执行出结果,但最后会报异常)
将相关jar包引入后,实际解析操作如下:
1.要解析的xml文件province.xml:
<china dn="day"> <city quName="黑龙江" pyName="heilongjiang" cityname="哈尔滨" state1="21" state2="7" stateDetailed="小到中雨转小雨" tem1="21" tem2="13" windState="东风4-5级转3-4级"/> <city quName="吉林" pyName="jilin" cityname="长春" state1="8" state2="9" stateDetailed="中雨转大雨" tem1="19" tem2="14" windState="东南风3-4级"/> <city quName="辽宁" pyName="liaoning" cityname="沈阳" state1="8" state2="9" stateDetailed="中雨转大雨" tem1="21" tem2="18" windState="东南风小于3级转3-4级"/> <city quName="海南" pyName="hainan" cityname="海口" state1="4" state2="1" stateDetailed="雷阵雨转多云" tem1="32" tem2="25" windState="东南风3-4级"/> <city quName="内蒙古" pyName="neimenggu" cityname="呼和浩特" state1="0" state2="0" stateDetailed="晴" tem1="24" tem2="12" windState="西南风转西北风3-4级"/> <city quName="新疆" pyName="xinjiang" cityname="乌鲁木齐" state1="0" state2="0" stateDetailed="晴" tem1="29" tem2="17" windState="微风"/> <city quName="西藏" pyName="xizang" cityname="拉萨" state1="3" state2="7" stateDetailed="阵雨转小雨" tem1="22" tem2="11" windState="微风"/> <city quName="青海" pyName="qinghai" cityname="西宁" state1="3" state2="3" stateDetailed="阵雨" tem1="20" tem2="10" windState="微风"/> <city quName="宁夏" pyName="ningxia" cityname="银川" state1="2" state2="1" stateDetailed="阴转多云" tem1="27" tem2="14" windState="北风3-4级转微风"/> <city quName="甘肃" pyName="gansu" cityname="兰州" state1="3" state2="1" stateDetailed="阵雨转多云" tem1="25" tem2="15" windState="微风"/> <city quName="河北" pyName="hebei" cityname="石家庄" state1="7" state2="1" stateDetailed="小雨转多云" tem1="30" tem2="22" windState="北风小于3级"/> <city quName="河南" pyName="henan" cityname="郑州" state1="4" state2="2" stateDetailed="雷阵雨转阴" tem1="31" tem2="24" windState="微风"/> <city quName="湖北" pyName="hubei" cityname="武汉" state1="1" state2="3" stateDetailed="多云转阵雨" tem1="35" tem2="26" windState="微风"/> <city quName="湖南" pyName="hunan" cityname="长沙" state1="0" state2="0" stateDetailed="晴" tem1="36" tem2="28" windState="南风小于3级"/> <city quName="山东" pyName="shandong" cityname="济南" state1="9" state2="3" stateDetailed="大雨转阵雨" tem1="30" tem2="21" windState="南风3-4级转北风小于3级"/> <city quName="江苏" pyName="jiangsu" cityname="南京" state1="1" state2="1" stateDetailed="多云" tem1="35" tem2="27" windState="南风3-4级"/> <city quName="安徽" pyName="anhui" cityname="合肥" state1="0" state2="1" stateDetailed="晴转多云" tem1="35" tem2="27" windState="西南风3-4级"/> <city quName="山西" pyName="shanxi" cityname="太原" state1="1" state2="0" stateDetailed="多云转晴" tem1="27" tem2="16" windState="微风转西北风小于3级"/> <city quName="陕西" pyName="sanxi" cityname="西安" state1="21" state2="1" stateDetailed="小到中雨转多云" tem1="25" tem2="20" windState="西风小于3级"/> <city quName="四川" pyName="sichuan" cityname="成都" state1="22" state2="22" stateDetailed="中到大雨" tem1="28" tem2="23" windState="北风3-4级"/> <city quName="云南" pyName="yunnan" cityname="昆明" state1="1" state2="1" stateDetailed="多云" tem1="27" tem2="17" windState="微风"/> <city quName="贵州" pyName="guizhou" cityname="贵阳" state1="1" state2="1" stateDetailed="多云" tem1="29" tem2="21" windState="南风小于3级"/> <city quName="浙江" pyName="zhejiang" cityname="杭州" state1="1" state2="1" stateDetailed="多云" tem1="35" tem2="26" windState="东风小于3级"/> <city quName="福建" pyName="fujian" cityname="福州" state1="1" state2="1" stateDetailed="多云" tem1="36" tem2="26" windState="微风"/> <city quName="江西" pyName="jiangxi" cityname="南昌" state1="0" state2="0" stateDetailed="晴" tem1="35" tem2="27" windState="微风"/> <city quName="广东" pyName="guangdong" cityname="广州" state1="0" state2="0" stateDetailed="晴" tem1="35" tem2="25" windState="微风"/> <city quName="广西" pyName="guangxi" cityname="南宁" state1="1" state2="1" stateDetailed="多云" tem1="35" tem2="25" windState="南风小于3级"/> <city quName="北京" pyName="beijing" cityname="北京" state1="7" state2="1" stateDetailed="小雨转多云" tem1="28" tem2="22" windState="微风"/> <city quName="天津" pyName="tianjin" cityname="天津" state1="3" state2="1" stateDetailed="阵雨转多云" tem1="28" tem2="23" windState="南风转西北风3-4级"/> <city quName="上海" pyName="shanghai" cityname="上海" state1="1" state2="1" stateDetailed="多云" tem1="35" tem2="28" windState="南风3-4级"/> <city quName="重庆" pyName="chongqing" cityname="重庆" state1="1" state2="8" stateDetailed="多云转中雨" tem1="36" tem2="23" windState="微风"/> <city quName="香港" pyName="xianggang" cityname="香港" state1="1" state2="1" stateDetailed="多云" tem1="33" tem2="27" windState="微风"/> <city quName="澳门" pyName="aomen" cityname="澳门" state1="1" state2="1" stateDetailed="多云" tem1="34" tem2="27" windState="微风"/> <city quName="台湾" pyName="taiwan" cityname="台北" state1="7" state2="10" stateDetailed="小雨转暴雨" tem1="32" tem2="27" windState="微风"/> <city quName="西沙" pyName="xisha" cityname="西沙" state1="8" state2="8" stateDetailed="中雨" tem1="31" tem2="26" windState="东北风转西风5-6级"/> <city quName="南沙" pyName="nanshadao" cityname="南沙" state1="4" state2="4" stateDetailed="雷阵雨" tem1="32" tem2="27" windState="西南风5-6级"/> <city quName="钓鱼岛" pyName="diaoyudao" cityname="钓鱼岛" state1="2" state2="1" stateDetailed="阴转多云" tem1="28" tem2="26" windState="东风4-5级转东南风5-6级"/> </china>
2、具体解析程序如下:
package Demo2; import java.io.File; import java.util.Iterator; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class XMLTest01 { public static void main(String args[]) throws DocumentException { SAXReader reader = new SAXReader(); Document document = reader.read(new File("D:/province.xml")); Element rootElm = document.getRootElement(); //Element root1Elm = rootElm.element("city"); @SuppressWarnings("rawtypes") List nodes = rootElm.elements("city"); for (@SuppressWarnings("rawtypes") Iterator it = nodes.iterator(); it.hasNext();) { Element elm = (Element) it.next(); System.out.println("quName:" + elm.attributeValue("quName") + " pyName:" + elm.attributeValue("pyName") + " cityname:" + elm.attributeValue("cityname") + " state1:" + elm.attributeValue("state1") + " state2:" + elm.attributeValue("state2")); } try { Document doc = reader.read(new File("D:/province.xml")); @SuppressWarnings("rawtypes") List projects = doc.selectNodes("china/city"); @SuppressWarnings("rawtypes") Iterator it = projects.iterator(); while (it.hasNext()) { Element elm = (Element) it.next(); System.out.println("quName:" + elm.attributeValue("quName") + " pyName:" + elm.attributeValue("pyName") + " cityname:" + elm.attributeValue("cityname") + " state1:" + elm.attributeValue("state1") + " state2:" + elm.attributeValue("state2")); } } catch (Exception ex) { ex.printStackTrace(); } } }
3、项目中需要导入的架包:
4、运行结果如下图所示:
java dom4j解析xml实例(2),布布扣,bubuko.com
标签:des style blog http java 使用
原文地址:http://www.cnblogs.com/henuyuxiang/p/3841182.html