首頁 Java java教程 使用dom4j解析xml文檔實例

使用dom4j解析xml文檔實例

May 29, 2018 pm 05:57 PM
加強 基礎


.



使用dom4j解析xml文档
*  1.SAXReader对象(dom4j核心类)
a) read(…) 加载执行xml文档
2. Document对象
a) Element e = getRootElement() 获得根元素
3. Element对象
a) Element [] eleArr = elements(…) 获得指定名称的所有子元素。可以不指定名称
b) element(…) 获得指定名称第一个子元素。可以不指定名称
c) getName() 获得当前元素的元素名
d) attributeValue(…) 获得指定属性名的属性值
e) elementText(…) 获得指定名称子元素的文本值
f) getText() 获得当前元素的文本内容
 操作步骤:
  1.创建dom4j核心对象SAXReader
  2.使用SAXReader中read方法读取xml文档,生成Document对象
  3.使用Document中方法getRootElement获取根元素Element
  4.使用Element中的方法elements获取所有bean元素
  5.遍历包含bean元素的集合,获取每一个bean元素
  6.使用Element中的方法attributeValue获取bean元素上属性的值
  7.使用Element中的方法elements获取所有property元素
  8.遍历包含property元素的集合,获取每一个property元素
9.使用Element中的方法attributeValue获取property元素上属性的值
10.使用Element中的方法getText获取获取property元素上文本值

 1 public class UseDom4jParseXML { 
 2     public static void main(String[] args) throws Exception { 
 3         //1.创建dom4j核心对象SAXReader 
 4         SAXReader sax = new SAXReader(); 
 5         //2.使用SAXReader中read方法读取xml文档,生成Document对象 
 6         Document docu = sax.read("bean.xml"); 
 7         //3.使用Document中方法getRootElement获取根元素Element 
 8         Element rootElement = docu.getRootElement(); 
 9         //4.使用Element中的方法elements获取所有bean元素
 10         List<Element> beanElementList = rootElement.elements();
 11         //5.遍历包含bean元素的集合,获取每一个bean元素
 12         for (Element beanElement : beanElementList) {
 13             String beanName = beanElement.getName();
 14             System.out.println(beanName);
 15             //6.使用Element中的方法attributeValue获取bean元素上属性的值
 16             String idValue = beanElement.attributeValue("id");
 17             System.out.println("\tbean元素的属性id:"+idValue);
 18             String classNameValue = beanElement.attributeValue("className");
 19             System.out.println("\tbean元素的属性className:"+classNameValue);
 20             //7.使用Element中的方法elements获取所有property元素
 21             List<Element> propertyElementList = beanElement.elements();
 22             //8.遍历包含property元素的集合,获取每一个property元素
 23             for (Element propertyElement : propertyElementList) {
 24                 System.out.println("\t\t"+propertyElement.getName());
 25                 //9.使用Element中的方法attributeValue获取property元素上属性的值
 26                 String nameValue = propertyElement.attributeValue("name");
 27                 System.out.println("\t\t\tproperty元素的属性name:"+nameValue);
 28                 String valueValue = propertyElement.attributeValue("value");
 29                 System.out.println("\t\t\tproperty元素的属性value:"+valueValue);
 30                 //10.使用Element中的方法getText获取获取property元素上文本值
 31                 String text = propertyElement.getText();
 32                 System.out.println("\t\t\tproperty元素的文本:"+text);
 33             }
 34         }
 35     }
 36 }
登入後複製

BeanUtils工具类
使用BeanUitls公共,给类中的成员变量注入(赋)值

 1 /*  
 2  * 创建MyBeanUtils工具类,增强populate方法  
 3  */  
 4 public class MyBeanUtils {  
 5     //把构造方法私有,不让外界通过创建对象的方式调用方法  
 6     private MyBeanUtils() {  
 7     }  
 8       
 9     /* 
 10      * 定义一个方法(让用户使用自己定义的populate方法不用处理异常) 
 11      *     1.参数传递JavaBean对象的Class文件对象 
 12      *     2.内部通过反射创建Javabean对象 
 13      *     3.调用BeanUtils工具类的方法populate 
 14      *     4.对populate方法的异常进行try...catch处理 
 15      *     5.把对象返回给用户 
 16      *     6.把参数Class对象增加一个泛型,让用户传递什么类型的JavaBean,就返回什么类型的JavaBean 
 17      * 
 18      * 定义含有泛型的方法:调用方法时确定数据类型 
 19      *         修饰符 <定义泛型> 返回值类型 方法名(参数<使用泛型>){ 
 20      *             方法体 
 21      *         } 
 22      * 
 23      * 
 24      * 方法的参数: 
 25      *         Class clazz 
 26      *         Map<String,String[]> properties 
 27      * 方法的返回值类型: 
 28      *         Object 
 29      */ 
 30     public static <E> E populate03(Class<E> clazz, Map<String,String[]> properties){ 
 31         try { 
 32             //2.内部通过反射创建Javabean对象 
 33             E obj = clazz.newInstance(); 
 34             //3.调用BeanUtils工具类的方法populate 
 35             BeanUtils.populate(obj, properties); 
 36             //5.把对象返回给用户 
 37             return obj; 
 38         } catch (Exception e) { 
 39             //4.对populate方法的异常进行try...catch处理
 40             e.printStackTrace(); 
 41             //把编译异常,转换为运行时异常,给成员变量注入值失败,让程序停止下来 
 42             throw new RuntimeException("注入值失败"); 
 43         } 
 44     } 
 45      
 46     /* 
 47      * 定义一个方法(让用户使用自己定义的populate方法不用处理异常) 
 48      *     1.参数传递JavaBean对象的Class文件对象 
 49      *     2.内部通过反射创建Javabean对象 
 50      *     3.调用BeanUtils工具类的方法populate 
 51      *     4.对populate方法的异常进行try...catch处理 
 52      *     5.把对象返回给用户 
 53      * 
 54      * 方法的参数: 
 55      *         Class clazz 
 56      *         Map<String,String[]> properties 
 57      * 方法的返回值类型: 
 58      *         Object 
 59      */ 
 60     public static Object populate02(Class clazz, Map<String,String[]> properties){ 
 61         try { 
 62             //2.内部通过反射创建Javabean对象 
 63             Object obj = clazz.newInstance(); 
 64             //3.调用BeanUtils工具类的方法populate 
 65             BeanUtils.populate(obj, properties); 
 66             //5.把对象返回给用户 
 67             return obj; 
 68         } catch (Exception e) { 
 69             //4.对populate方法的异常进行try...catch处理 
 70             e.printStackTrace(); 
 71             //把编译异常,转换为运行时异常,给成员变量注入值失败,让程序停止下来 
 72             throw new RuntimeException("注入值失败"); 
 73         } 
 74          
 75     } 
 76      
 77     /* 
 78      * 定义一个方法(让用户使用自己定义的populate方法不用处理异常) 
 79      *     1.调用BeanUtils工具类的方法populate 
 80      *     2.对populate方法的异常进行try...catch处理 
 81      * 
 82      * 方法的参数: 
 83      *         Object bean 
 84      *         Map<String,String[]> properties 
 85      * 方法的返回值类型: 
 86      *         void 
 87      */ 
 88     public static void populate01(Object bean, Map<String,String[]> properties){ 
 89         try { 
 90             //1.调用BeanUtils工具类的方法populate 
 91             BeanUtils.populate(bean, properties); 
 92         } catch (Exception e) { 
 93             //2.对populate方法的异常进行try...catch处理 
 94             e.printStackTrace(); 
 95             //把编译异常,转换为运行时异常,给成员变量注入值失败,让程序停止下来 
 96             throw new RuntimeException("注入值失败"); 
 97         } 
 98     } 
 99 }
 100 package cn.itcast.dmeo03.MyBeanUtils;
 101 
 102 import java.util.HashMap;
 103 import java.util.Map;
 104 
 105 import org.junit.Test;
 106 
 107 import cn.itcast.dmeo01.bean.User;
 108 
 109 /*
 110  * 使用自定义工具类MyBeanUtils
 111  */
 112 public class UseMyBeanUtils {
 113     @Test
 114     public void demo03(){
 115         //创建Map集合,key是String类型,value是String类型的数组
 116         Map<String,String[]> properties = new HashMap<String,String[]>();
 117         properties.put("id", new String[]{"123"});
 118         properties.put("username", new String[]{"root","admin"});
 119         properties.put("password", new String[]{"root","123456"});
 120         properties.put("hobbies", new String[]{"吃","睡","玩","敲代码"});
 121         //调用MyBeanUtils工具类中的方法populate02
 122         User u = MyBeanUtils.populate03(User.class, properties);
 123         System.out.println(u);
 124     }
 125     
 126     @Test
 127     public void demo02(){
 128         //创建Map集合,key是String类型,value是String类型的数组
 129         Map<String,String[]> properties = new HashMap<String,String[]>();
 130         properties.put("id", new String[]{"123"});
 131         properties.put("username", new String[]{"root","admin"});
 132         properties.put("password", new String[]{"root","123456"});
 133         properties.put("hobbies", new String[]{"吃","睡","玩","敲代码"});
 134         //调用MyBeanUtils工具类中的方法populate02
 135         User u = (User) MyBeanUtils.populate02(User.class, properties);
 136         System.out.println(u);
 137     }
 138     
 139     @Test
 140     public void demo01(){
 141         //创建JavaBean对象
 142         User u = new User();
 143         //创建Map集合,key是String类型,value是String类型的数组
 144         Map<String,String[]> properties = new HashMap<String,String[]>();
 145         properties.put("id", new String[]{"123"});
 146         properties.put("username", new String[]{"root","admin"});
 147         properties.put("password", new String[]{"root","123456"});
 148         properties.put("hobbies", new String[]{"吃","睡","玩","敲代码"});
 149         //调用MyBeanUtils工具类中的方法populate01
 150         MyBeanUtils.populate01(u, properties);
 151         System.out.println(u);
 152     }
 153 }
登入後複製
 1 /* 
 2  * 综合案例:XML+dom4j+反射+BeanUtils 
 3  *     1.使用xml存储JavaBean的全类名和属性名属性值 
 4  *     2.使用dom4j解析xml 
 5  *     3.使用反射技术根据解析出的全类名创建JavaBean对象 
 6  *     4.使用BeanUtils中的方法setProperty给成员变量注入值 
 7  */ 
 8 public class UseDom4jparseXMLToJavaBean { 
 9     public static void main(String[] args) throws Exception {
 10         //2.使用dom4j解析xml
 11         //获取dom4j的核心类SAXReader
 12         SAXReader sax = new SAXReader();
 13         //使用SAXReader中的read读取xml,创建Document对象
 14         Document docu = sax.read("src/cn/itcast/dmeo05/domain/data.xml");
 15         //使用Document中的方法getRootElement获取根元素
 16         Element rootElement = docu.getRootElement();
 17         //使用Element中的方法elements获取所有的bean元素,放入集合中
 18         List<Element> beanElementList = rootElement.elements();
 19         //遍历beanElementList集合
 20         for (Element beanElement : beanElementList) {
 21             //获取beanElement上的变的属性className
 22             String className = beanElement.attributeValue("className");
 23             //3.使用反射技术根据解析出的全类名创建JavaBean对象
 24             Class clazz = Class.forName(className);
 25             Object obj = clazz.newInstance();
 26             //使用Element中的方法elements获取所有的property元素,放入集合中
 27             List<Element> propertyElementList = beanElement.elements();
 28             //遍历propertyElementList集合
 29             for (Element propertyElement : propertyElementList) {
 30                 //获取propertyElement上的属性name(属性名)和value(属性值)
 31                 String name = propertyElement.attributeValue("name");
 32                 String value = propertyElement.attributeValue("value");
 33                 //4.使用BeanUtils中的方法setProperty给成员变量注入值
 34                 BeanUtils.setProperty(obj, name, value);
 35             }
 36             //打印JavaBean对象
 37             System.out.println(obj);
 38         }
 39     }
 40 
 41 }
登入後複製

以上是使用dom4j解析xml文檔實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

PHP基礎教學:從入門到精通 PHP基礎教學:從入門到精通 Jun 18, 2023 am 09:43 AM

PHP是一種廣泛使用的開源伺服器端腳本語言,它可以處理Web開發中所有的任務。 PHP在網頁開發的應用廣泛,尤其是在動態資料處理上表現優異,因此被許多開發者喜愛和使用。在本篇文章中,我們將一步步講解PHP基礎知識,幫助初學者從入門到精通。一、基本語法PHP是一種解釋性語言,其程式碼類似HTML、CSS和JavaScript。每個PHP語句都以分號;結束,註

Win10麥克風加強拉不動怎麼辦 Win10麥克風加強拉不動怎麼辦 Jul 05, 2023 pm 06:13 PM

  Win10麥克風加強拉不動怎麼辦?很多小夥伴在使用電腦的時候經常會用到麥克風,而且其中一部分用戶在準備加強麥克風的時候卻頻繁出現了拉不動的情況,那麼我們在遇到這種情況的時候該怎麼辦呢?下面就跟著小編一起來看看Win10麥克風加強拉不動解決方法吧。  Win10麥克風加強拉不動解決方法  1、可以重新更新一下驅動試一下。  2、WIN7以上的系統,右下角的小喇叭右鍵錄音設備在錄音中把現有麥克風調成預設設備。然後雙擊麥克風或點屬性在等級裡可以調麥克風的音量大小和麥克風加強(這個適當調節,調大有

Linux可以零基礎學習嗎?需要學什麼? Linux可以零基礎學習嗎?需要學什麼? Feb 19, 2024 pm 12:57 PM

  想要從事IT行業,但是有不想要學習程式設計該選擇哪門技術合適呢?當然是Linux運維了。 Linux是市場上非常受歡迎的技術,應用範圍廣泛,就業前景好,受到了很多人的喜歡。那麼問題來了,Linux運維零基礎可以學習嗎?  在伺服器市場上,Linux系統因為穩定安全、免費開源和高效便捷等優點在市場佔有率高達80%,由此可以看得出來Linux應用是非常廣泛的。無論是現在還是未來,學習Linux都是非常好的選擇。至於零基礎可以學嗎?我的答案是當然可以了。老男孩教育Linux面授班專門針對零基礎人員設

學習Go語言變數的基礎知識 學習Go語言變數的基礎知識 Mar 22, 2024 pm 09:39 PM

Go語言是一種由Google開發的靜態類型、編譯型語言,其簡潔、高效的特性受到了廣泛的開發者關注和喜愛。在學習Go語言的過程中,熟練變數的基礎知識是至關重要的一步。本文將透過具體的程式碼範例來講解Go語言中變數的定義、賦值、類型推論等基礎知識,幫助讀者更能理解和掌握這些知識點。在Go語言中,定義一個變數可以使用關鍵字var,即var變數名變數類型的格

PHP基礎入門:如何使用echo函數輸出文字內容 PHP基礎入門:如何使用echo函數輸出文字內容 Jul 30, 2023 pm 05:38 PM

PHP基礎入門:如何使用echo函數輸出文字內容在PHP程式設計中,常常需要在網頁上輸出一些文字內容,這時就可以使用echo函數。本文將介紹如何使用echo函數輸出文字內容,並提供一些範例程式碼。在開始之前,首先要確保你已經安裝了PHP,並且配置了運行環境。如果還沒有安裝PHP,你可以在PHP官方網站(https://www.php.net)上下載最新的穩定版本。

C語言函數詳解:基礎到進階,全面解析函數的使用 C語言函數詳解:基礎到進階,全面解析函數的使用 Feb 18, 2024 pm 02:25 PM

C語言函數大全:從基礎到進階,詳解函數的使用方法,需要具體程式碼範例簡介:C語言是一種廣泛使用的程式語言,其強大的功能和靈活性使它成為許多開發人員的首選。在C語言中,函數是一個重要的概念,它能夠將一段程式碼組合成一個獨立的模組,提高了程式碼的重用性和可維護性。本文將從基礎開始介紹C語言函數的使用方法,並逐步進階,幫助讀者掌握函數編寫的技巧。一、函數的定義與呼叫在C

PHP學習筆記:物件導向程式設計基礎 PHP學習筆記:物件導向程式設計基礎 Oct 09, 2023 pm 12:46 PM

PHP學習筆記:物件導向程式設計基礎,需要具體程式碼範例導言:物件導向程式設計(Object-OrientedProgramming,簡稱OOP)是一種程式設計的思考方式,透過將問題分解為多個物件並定義物件之間的交互,來解決複雜的程式設計問題。 PHP作為一門功能強大的程式語言,也支援物件導向程式設計。本文將介紹PHP中物件導向程式設計的基礎概念和常用語法,同時提供具體的程式碼範例。類別

PHP學習筆記:基礎語法及變數定義 PHP學習筆記:基礎語法及變數定義 Oct 09, 2023 am 08:03 AM

PHP學習筆記:基礎語法及變數定義在現今的網路時代,PHP(HypertextPreprocessor)作為一種廣泛使用的伺服器腳本語言,被越來越多的開發者所青睞。本篇文章將為大家介紹PHP的基礎文法和變數的定義,並提供具體的程式碼範例,幫助初學者更能理解和掌握。一、PHP的基礎語法PHP程式碼的標記在PHP程式碼中,我們常常使用「&lt;?php」和「?&

See all articles