java dom4j解析xml实例
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文件data.xml:
<?xml version="1.0" encoding="UTF-8"?> <ReturnInfo> <userlist> <item index="1" level="60" nickname="凄惨的老生" country="金" weiwang="136929" /> <item index="2" level="85" nickname="哭泣的至尊宝" country="金" weiwang="97490" /> <item index="3" level="85" nickname="老公" country="金" weiwang="91103" /> <item index="4" level="78" nickname="冉闵" country="金" weiwang="82828" /> <item index="5" level="55" nickname="erggh" country="金" weiwang="77440" /> <item index="6" level="60" nickname="孤峰" country="金" weiwang="67821" /> <item index="7" level="60" nickname="乱世" country="金" weiwang="67675" /> <item index="8" level="62" nickname="叛逆之神" country="金" weiwang="61839" /> <item index="9" level="60" nickname="双乔主人" country="金" weiwang="61647" /> <item index="10" level="60" nickname="视觉效果" country="金" weiwang="50200" /> <item index="11" level="60" nickname="鬼鬼" country="金" weiwang="49537" /> <item index="12" level="59" nickname="狂龙在天" country="金" weiwang="46687" /> <item index="13" level="60" nickname="小子" country="金" weiwang="46152" /> <item index="14" level="52" nickname="zzy" country="金" weiwang="39017" /> <item index="15" level="53" nickname="ycf" country="金" weiwang="30333" /> <item index="16" level="52" nickname="无界之魂" country="金" weiwang="20294" /> <item index="17" level="42" nickname="刀锋狼" country="金" weiwang="18842" /> <item index="18" level="60" nickname="mnbvc" country="金" weiwang="18102" /> <item index="19" level="41" nickname="赢政" country="金" weiwang="16331" /> <item index="20" level="52" nickname="徐匡胤" country="金" weiwang="16156" /> </userlist> </ReturnInfo>
2、具体解析程序如下:
package demo4; 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 TestJava { public static void main(String args[]) throws DocumentException { SAXReader reader = new SAXReader(); Document document = reader.read(new File("D:/data.xml")); Element rootElm = document.getRootElement(); Element root1Elm = rootElm.element("userlist"); List nodes = root1Elm.elements("item"); for (Iterator it = nodes.iterator(); it.hasNext();) { Element elm = (Element) it.next(); System.out.println("index:" + elm.attributeValue("index") + " level:" + elm.attributeValue("level") + " nickname:" + elm.attributeValue("nickname") + " country:" + elm.attributeValue("country") + " weiwang:" + elm.attributeValue("weiwang")); } try { Document doc = reader.read(new File("D:/data.xml")); List projects = doc.selectNodes("ReturnInfo/userlist/item"); Iterator it = projects.iterator(); while (it.hasNext()) { Element elm = (Element) it.next(); System.out.println("index:" + elm.attributeValue("index") + " level:" + elm.attributeValue("level") + " nickname:" + elm.attributeValue("nickname") + " country:" + elm.attributeValue("country") + " weiwang:" + elm.attributeValue("weiwang")); } } catch (Exception ex) { ex.printStackTrace(); } } }
3、运行结果如下图所示:
<1>、jaxen-1.1-beta-7.jar架包导入后不报异常,正常得到结果:
<2>、jaxen-1.1-beta-7.jar架包不导入后报异常,但也能得到结果: