0

0

使用dom4j解析xml文档实例

零下一度

零下一度

发布时间:2018-05-29 17:57:51

|

2165人浏览过

|

来源于php中文网

原创


.



使用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公共,给类中的成员变量注入(赋)值

aspx1财付通支付接口源码
aspx1财付通支付接口源码

本支付接口的特点,主要是用xml文件来记录订单详情和支付详情。代码比较简单,只要将里面的商户号、商户key换成你自己的,将回调url换成你的网站,就可以使用了。通过这个实例也可以很好的了解一般在线支付接口的基本工作原理。其中的pay.config文件记录的是支付详情,order.config是订单详情

下载
 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);
 124124     }
 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 }

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
Golang 测试体系与代码质量保障:工程级可靠性建设
Golang 测试体系与代码质量保障:工程级可靠性建设

Go语言测试体系与代码质量保障聚焦于构建工程级可靠性系统。本专题深入解析Go的测试工具链(如go test)、单元测试、集成测试及端到端测试实践,结合代码覆盖率分析、静态代码扫描(如go vet)和动态分析工具,建立全链路质量监控机制。通过自动化测试框架、持续集成(CI)流水线配置及代码审查规范,实现测试用例管理、缺陷追踪与质量门禁控制,确保代码健壮性与可维护性,为高可靠性工程系统提供质量保障。

28

2026.02.28

Golang 工程化架构设计:可维护与可演进系统构建
Golang 工程化架构设计:可维护与可演进系统构建

Go语言工程化架构设计专注于构建高可维护性、可演进的企业级系统。本专题深入探讨Go项目的目录结构设计、模块划分、依赖管理等核心架构原则,涵盖微服务架构、领域驱动设计(DDD)在Go中的实践应用。通过实战案例解析接口抽象、错误处理、配置管理、日志监控等关键工程化技术,帮助开发者掌握构建稳定、可扩展Go应用的最佳实践方法。

23

2026.02.28

Golang 性能分析与运行时机制:构建高性能程序
Golang 性能分析与运行时机制:构建高性能程序

Go语言以其高效的并发模型和优异的性能表现广泛应用于高并发、高性能场景。其运行时机制包括 Goroutine 调度、内存管理、垃圾回收等方面,深入理解这些机制有助于编写更高效稳定的程序。本专题将系统讲解 Golang 的性能分析工具使用、常见性能瓶颈定位及优化策略,并结合实际案例剖析 Go 程序的运行时行为,帮助开发者掌握构建高性能应用的关键技能。

27

2026.02.28

Golang 并发编程模型与工程实践:从语言特性到系统性能
Golang 并发编程模型与工程实践:从语言特性到系统性能

本专题系统讲解 Golang 并发编程模型,从语言级特性出发,深入理解 goroutine、channel 与调度机制。结合工程实践,分析并发设计模式、性能瓶颈与资源控制策略,帮助将并发能力有效转化为稳定、可扩展的系统性能优势。

16

2026.02.27

Golang 高级特性与最佳实践:提升代码艺术
Golang 高级特性与最佳实践:提升代码艺术

本专题深入剖析 Golang 的高级特性与工程级最佳实践,涵盖并发模型、内存管理、接口设计与错误处理策略。通过真实场景与代码对比,引导从“可运行”走向“高质量”,帮助构建高性能、可扩展、易维护的优雅 Go 代码体系。

18

2026.02.27

Golang 测试与调试专题:确保代码可靠性
Golang 测试与调试专题:确保代码可靠性

本专题聚焦 Golang 的测试与调试体系,系统讲解单元测试、表驱动测试、基准测试与覆盖率分析方法,并深入剖析调试工具与常见问题定位思路。通过实践示例,引导建立可验证、可回归的工程习惯,从而持续提升代码可靠性与可维护性。

2

2026.02.27

漫蛙app官网链接入口
漫蛙app官网链接入口

漫蛙App官网提供多条稳定入口,包括 https://manwa.me、https

164

2026.02.27

deepseek在线提问
deepseek在线提问

本合集汇总了DeepSeek在线提问技巧与免登录使用入口,助你快速上手AI对话、写作、分析等功能。阅读专题下面的文章了解更多详细内容。

8

2026.02.27

AO3官网直接进入
AO3官网直接进入

AO3官网最新入口合集,汇总2026年可用官方及镜像链接,助你快速稳定访问Archive of Our Own平台。阅读专题下面的文章了解更多详细内容。

309

2026.02.27

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PostgreSQL 教程
PostgreSQL 教程

共48课时 | 10万人学习

Git 教程
Git 教程

共21课时 | 3.9万人学习

Django 教程
Django 教程

共28课时 | 4.7万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号