日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

dom4j解析xml实例(2)

發布時間:2025/3/21 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 dom4j解析xml实例(2) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

dom4j是一個java的XML API,類似jdom,用來讀寫XML文件,它性能優異、功能強大和極易使用等特點

所用jar包:dom4j-1.6.1.jar、jaxen-1.1-beta-6.jar

需要解析的xml文件:people.xml

<people city="shenzhen"> <student name="milton" age="22"></student> <student name="lego" age="23"></student> <teacher name="bruce" age="27"></teacher> <teacher name="ziven" age="28"></teacher> </people>

?

java代碼如下:

package demo5;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 Test01 {public static void main(String args[]) throws DocumentException {SAXReader reader = new SAXReader();Document document = reader.read(new File("D:/people.xml"));Element rootElm = document.getRootElement();//Element root1Elm = rootElm.element("city");@SuppressWarnings("rawtypes")List nodes = rootElm.elements("student");@SuppressWarnings("rawtypes")List nodess = rootElm.elements("teacher");for (@SuppressWarnings("rawtypes")Iterator it = nodes.iterator(); it.hasNext();) {Element elm = (Element) it.next();System.out.println("name:" + elm.attributeValue("name")+ " age:" + elm.attributeValue("age"));}for (@SuppressWarnings("rawtypes")Iterator it = nodess.iterator(); it.hasNext();) {Element elm = (Element) it.next();System.out.println("name:" + elm.attributeValue("name")+ " age:" + elm.attributeValue("age"));}System.out.println();try {Document doc = reader.read(new File("D:/people.xml"));@SuppressWarnings("rawtypes")List projects = doc.selectNodes("people/student");@SuppressWarnings("rawtypes")List projectss = doc.selectNodes("people/teacher");@SuppressWarnings("rawtypes")Iterator it = projects.iterator();while (it.hasNext()) {Element elm = (Element) it.next();System.out.println("name:" + elm.attributeValue("name")+ " age:" + elm.attributeValue("age"));}@SuppressWarnings("rawtypes")Iterator its = projectss.iterator();while (its.hasNext()) {Element elm = (Element) its.next();System.out.println("name:" + elm.attributeValue("name")+ " age:" + elm.attributeValue("age"));}} catch (Exception ex) {ex.printStackTrace();}}}

?

代碼運行后結果如下:

轉載于:https://www.cnblogs.com/henuyuxiang/p/3875086.html

總結

以上是生活随笔為你收集整理的dom4j解析xml实例(2)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。