工作原理如下: Digester底层采用SAX(Simple API for XML)析XML文件,所以很自然的,对象转换由"事件"驱动,在遍历每个节点时,检查是否有匹配模式,如果有,则执行规则定义的操作,比如创建特定的Java对象,或调用特定对象的方法等。此处的XML元素根据匹配模式(matching pattern)识别,而相关操作由规则(rule)定义。
接下来以例子说明
1.解析xml文件 view.xml
2.ViewCache类
3.Area类
4.测试类
接下来以例子说明
1.解析xml文件 view.xml
<?xml version="1.0" encoding="UTF-8" ?>
<viewcache name="test">
<area info="area1">
<id>1098</id>
<parentId>1001</parentId>
<areaType>province</areaType>
<name>北京</name>
<ordering>1867</ordering>
</area>
<area info="area2">
<id>1099</id>
<parentId>1098</parentId>
<areaType>capital</areaType>
<name>北京</name>
<ordering>1868</ordering>
<phoneArea>010</phoneArea>
</area>
<area info="area3">
<id>4476</id>
<parentId>1099</parentId>
<areaType>county</areaType>
<name>北京市朝阳区</name>
<ordering>1869</ordering>
<phoneArea>010</phoneArea>
</area>
<area info="area4">
<id>4477</id>
<parentId>1099</parentId>
<areaType>county</areaType>
<name>北京市崇文区</name>
<ordering>1870</ordering>
<phoneArea>010</phoneArea>
</area>
<area info="area5">
<id>4478</id>
<parentId>1099</parentId>
<areaType>county</areaType>
<name>北京市大兴区</name>
<ordering>1871</ordering>
<phoneArea>010</phoneArea>
</area>
</viewcache>
2.ViewCache类
import java.util.ArrayList;
import java.util.List;
public class ViewCache {
public String viewName;
public List<Area> areas = new ArrayList<Area>();
public String getViewName() {
return viewName;
}
public void setViewName(String viewName) {
this.viewName = viewName;
}
public List<Area> getAreas() {
return areas;
}
public void setAreas(List<Area> areas) {
this.areas = areas;
}
public void addArea(Area area){
areas.add(area);
}
}
3.Area类
public class Area {
public String id;
public String parentId;
public String areaType;
public String name;
public String ordering;
public String phoneArea;
public String info;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
public String getAreaType() {
return areaType;
}
public void setAreaType(String areaType) {
this.areaType = areaType;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getOrdering() {
return ordering;
}
public void setOrdering(String ordering) {
this.ordering = ordering;
}
public String getPhoneArea() {
return phoneArea;
}
public void setPhoneArea(String phoneArea) {
this.phoneArea = phoneArea;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
//测试解析方法用
public String setProperties(String id,String parentId,String areaType){
this.id = id;
this.parentId =parentId;
this.areaType = areaType;
return "id:"+id+",parentId:"+parentId+",areaType:"+areaType;
}
@Override
public String toString() {
return "id:"+id+",parentId:"+parentId+",areaType:"+areaType+",name:"+name
+",ordering:"+ordering+",phoneArea:"+phoneArea;
}
4.测试类
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.commons.digester.Digester;
import org.xml.sax.SAXException;
public class ViewMain {
public static void main(String[] args) throws IOException, SAXException {
// 建立一个Digester对象
Digester digester = new Digester();
//指定它不要用DTD验证XML文档的合法性——这是因为我们没有为XML文档定义DTD
digester.setValidating(false);
//创建一个模型对象,viewcache作为跟对象
digester.addObjectCreate("viewcache", ViewCache.class);
/*设置viewcache对象的属性,view.xml中的name对应ViewCache 属性 viewName;
如果属性多,可以调用digester.addSetProperties(pattern, attributeNames, propertyNames)方法*/
digester.addSetProperties("viewcache","name","viewName");
//创建下层节点模型对象
digester.addObjectCreate("viewcache/area", Area.class);
//设置属性 同上
digester.addSetProperties("viewcache/area","info","info");
//父节点和子节点直接的连接关系通过addSetNext方法实现。addArea为父节点类中的方法
digester.addSetNext("viewcache/area", "addArea");
/*解析子节点属性用addBeanPropertySetter方法,addBeanPropertySetter()是将子节点转换为对象的属性,
当对象的属性名和子节点的名字不一样时用来指定对象的属性名 */
digester.addBeanPropertySetter("viewcache/area/id", "id");
digester.addBeanPropertySetter("viewcache/area/parentId", "parentId");
digester.addBeanPropertySetter("viewcache/area/areaType", "areaType");
digester.addBeanPropertySetter("viewcache/area/name", "name");
digester.addBeanPropertySetter("viewcache/area/ordering", "ordering");
digester.addBeanPropertySetter("viewcache/area/phoneArea", "phoneArea");
//解析节点类中的方法通过addCallMethod,如果有参数,先在改方法中设置参数个数,再用addCallParam设置参数
digester.addCallMethod("viewcache/area", "setProperties", 3);
digester.addCallParam("viewcache/area", 0, "id");
digester.addCallParam("viewcache/area", 1, "parentId");
digester.addCallParam("viewcache/area", 2, "areaType");
File file = new File("D:/software/download/dubbo/sshDemo/src/com/sshdemo/executive/digister/view.xml");
ViewCache viewCache = (ViewCache)digester.parse(file);
List<Area> areas = viewCache.getAreas();
for(Area area : areas){
System.out.println("area number:"+area.getInfo());
System.out.println("area content:"+area.toString());
System.out.println("area method:"+area.setProperties("000", "999", "county"));
}
}
}