首页   快速返回

XML解析之Digester     所属分类 java
XML解析三种方式
DOM SAX STAX


DOM(Document Object Model,文档对象模型)
将整个xml 加载到内存,形成文档树结构对象,所有对xml操作都对内存中文档树结构对象进行。 

SAX Simple API for XML
一边解析 ,一边处理,一边释放内存资源
基于事件的模型,解析XML文档时触发一系列事件,当发现给定的tag时,激活一个回调方法



STAX  The Stream API for XML
   
SAX 推模式 
STAX 拉模式


Digester  processes an XML input stream by matching a series of element nesting patterns 
to execute Rules that have been added prior to the start of parsing. 
 



<dependency>
   <groupId>commons-digester</groupId>
   <artifactId>commons-digester</artifactId>
   <version>2.1</version>
</dependency>




User.java public class User{ private String code; private String name;
Deparment.java public class Department{ private String code; private String name; private Map extension = new HashMap(); private List users = new ArrayList(); public void addUser(User user){ users.add(user); } public void putExtension(String name,String value){ this.extension.put(name, value); }
data.xml <department code="deptcode001" name="deptname001"> <user code="usercode001" name="username001"></user> <user code="usercode002" name="username002"></user> <extension> <property-name>director</property-name> <property-value>tiger</property-value> </extension> </department>
Digester digester = new Digester(); digester.setValidating(false); digester.addObjectCreate("department", Department.class); digester.addSetProperties("department"); digester.addObjectCreate("department/user", User.class); digester.addSetProperties("department/user"); digester.addSetNext("department/user", "addUser"); // void addCallMethod(String pattern, String methodName,int paramCount) digester.addCallMethod("department/extension", "putExtension",2); digester.addCallParam("department/extension/property-name", 0); digester.addCallParam("department/extension/property-value", 1); String file = "data.xml"; Department department = (Department)digester.parse(new File(file));

上一篇     下一篇
tomcat8.5架构概述

tomcat8.5启动过程远程调试

tomcat8.5 jsp编译过程

tomcat8.5启动过程调试

fastjson使用技巧

java获取当前jvm进程id