Digester框架属于Jakarta Commons,它以规则和模式为基础处理XML文档。与SAX和DOM之类的标准API相比,Digester不涉及太多的细节问题,非常适合于对XML文档进行简单的处理。
比如要解析如下的xml文件
<persons>
<person name="tom">
<age>4</age>
<address>
<street>no1street</street>
<belongarea>us</belongarea>
</address>
<creditcard>
<limit code="123" pwd="abc">1000</limit>
<bank>ICBC</bank>
</creditcard>
<creditcard>
<limit code="321" pwd="cba">2000</limit>
<bank>CBC</bank>
</creditcard>
</person>
<person name="jerry">
<age>5</age>
<address>
<street>no2street</street>
<belongarea>us</belongarea>
</address>
</person>
</persons>
首先定义address对应的类
public class Address {
private String street;
private String belongArea;
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getBelongArea() {
return belongArea;
}
public void setBelongArea(String belongArea) {
this.belongArea = belongArea;
}
@Override
public String toString(){
return "street:" +this.getStreet() + ", belongarea:" + this.getBelongArea();
}
}
接着定义creditcard对应的类
public class Creditcard {
private int code;
private String pwd;
private String limit;
private String bank;
public void setParam(String limit, String bank){
this.limit = limit;
this.bank = bank;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getLimit() {
return limit;
}
public void setLimit(String limit) {
this.limit = limit;
}
public String getBank() {
return bank;
}
public void setBank(String bank) {
this.bank = bank;
}
@Override
public String toString(){
return "limit code:" + this.getCode() + ", pwd:" + this.getPwd() + ", limitValue:" + this.getLimit() + ", bank:" + this.getBank();
}
}
接下来定义person对应的类
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2016/7/1 0001.
*/
public class Person {
private String name;
private int age;
private Address address;
private List<Creditcard> creditcard = new ArrayList<Creditcard>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public List<Creditcard> getCreditcard() {
return creditcard;
}
public void setCreditcard(List<Creditcard> creditcard) {
this.creditcard = creditcard;
}
public void addCreditcard(Creditcard creditcard){
this.creditcard.add(creditcard);
}
@Override
public String toString(){
return "name:" + this.getName() + ", age:" + this.getAge() + "\naddress:" + this.getAddress() + "\ncreditcare:" + this.getCreditcard();
}
}
最后定义persons对应的类
public class Root {
private List<Person> person = new ArrayList<Person>();
public List<Person> getPerson() {
return person;
}
public void setPerson(List<Person> person) {
this.person = person;
}
public void addPerson(Person person){
this.person.add(person);
}
@Override
public String toString(){
for(Person person : this.getPerson()){
System.out.print( "person: " + person + "\n");
}
return "";
}
}
需要注意的是,所有的对象都必须是public类型重点内容
然后来写解析文件
import org.apache.commons.digester.Digester;
import org.xml.sax.SAXException;
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
//digester
Digester digester = new Digester();
digester.setValidating(false);
digester.addObjectCreate("persons",Root.class);
digester.addObjectCreate("persons/person", Person.class);
//name
digester.addSetProperties("persons/person", "name", "name");
//age
digester.addBeanPropertySetter("persons/person/age");
//address
digester.addObjectCreate("persons/person/address",Address.class);
digester.addBeanPropertySetter("persons/person/address/street");
digester.addBeanPropertySetter("persons/person/address/belongarea","belongArea");
digester.addSetNext("persons/person/address","setAddress");
//creditcard
digester.addObjectCreate("persons/person/creditcard",Creditcard.class);
digester.addCallMethod("persons/person/creditcard","setParam",2);
digester.addCallParam("persons/person/creditcard/limit",0);
digester.addCallParam("persons/person/creditcard/bank",1);
/*实现跟上面3条语句同样的功能
digester.addBeanPropertySetter("persons/person/creditcard/limit","limit");
digester.addBeanPropertySetter("persons/person/creditcard/bank","bank");*/
digester.addSetProperties("persons/person/creditcard/limit","code","code");
digester.addSetProperties("persons/person/creditcard/limit","pwd","pwd");
digester.addSetNext("persons/person/creditcard","addCreditcard");
digester.addSetNext("persons/person","addPerson");
Root root;
try {
root = (Root)digester.parse(new File("C:\\file3.xml"));
System.out.println(root);
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
}
1、首先生成Digester对象,并且设置不进行dtd校验
2、addObjectCreate方法是解析标签,第一个参数是对应于xml的标签,第二个参数是Java Bean的Class
这个方法会将一个Class放进栈中
3、addSetProperties方法是解析属性,第一个参数是对应xml的标签,第二个参数是属性名,第三个参数是Bean中的变量名。
4、addBeanProperties方法是解析元素,第一个参数是对应元素,第二个参数是Bean中的变量名,当他们相同时,可以省略。
5、addSetNext方法是解析列表,当一个Class解析完,必须调用,参数一是对应标签,参数二有两种情况,如果有相同的列表,则是add方法,如果没有,就是set方法,相当于出栈。
6、addCallMethod方法,是直接显式调用bean中的方法,参数一是对应标签,参数二是bean中方法名,参数三是方法参数个数。当参数三为0时表示,这个方法有一个参数,取值来自这个标签。当参数三不为0时,要通过addCallParam方法指定参数取值。
addCallParam方法有两个参数,参数1是对应元素,参数二是指当前元素的值作为第几个参数传入addCallMethod方法中。
7、parse方法,传入解析的xml文件。