java dom4j解析xml实例(2)
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <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、具体解析程序如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | 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、运行结果如下图所示:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix