commons-digester

官网 http://commons.apache.org/proper/commons-digester/

依赖jar包
Digester 3.0 + Logging 1.1.1 + BeanUtils 1.8.3
Digester底层采用SAX解析XML文件
这个Digester 是很强大的,tomcat xml 解析就是他,strusts的也是他,
Digester 的实体必须属性必须实现get,set方法,满足bean的要求
digester 首先解析定义规则
xml的层次
classes
classes/student
classes/student/address

<?xml version="1.0" encoding="UTF-8" ?>
<classes name="四年级" description="超级牛逼">
    <student name="张三" age="20">
        <address streetName="武侯区" streetNumber="223"/>
    </student>
    <student name="李四" age="18">
        <address streetName="青羊区" streetNumber="900"/>
    </student>
</classes>
public class Classes {
    private String name;
    private String description;

    private ArrayList<Student> students = new ArrayList();

    public void addStudents(Student student) {
        this.students.add(student);
    }

    @Override
    public String toString() {
        return "Classes{" +
                "name='" + name + '\'' +
                ", description='" + description + '\'' +
                ", students=" + students +
                '}';
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
public class Student {
    private String name;
    private int age;
    private Address address;

    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;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address=" + address +
                '}';
    }
}
public class Address {
    private String streetName;
    private String streetNumber;

    public String getStreetName() {
        return streetName;
    }

    public void setStreetName(String streetName) {
        this.streetName = streetName;
    }

    public String getStreetNumber() {
        return streetNumber;
    }

    public void setStreetNumber(String streetNumber) {
        this.streetNumber = streetNumber;
    }

    @Override
    public String toString() {
        return "Address{" +
                "streetName='" + streetName + '\'' +
                ", streetNumber='" + streetNumber + '\'' +
                '}';
    }
}

public class DigesterTest {

    public static void main(String[] args) throws Exception {
        URL resource = DigesterTest.class.getClassLoader().getResource("com/digester/classes.xml");

        Digester digester = new Digester();

        digester.addObjectCreate("classes",Classes.class);//创建一个Classes对象
        digester.addSetProperties("classes");//把classes节点全把设置到实体上
        digester.addObjectCreate("classes/student", Student.class);//创建一个Student对象
        digester.addSetProperties("classes/student");//把student节点全把设置到实体上
        digester.addSetNext("classes/student","addStudents");//把节点student,调用addStdents 添加到Classes 对象上

        digester.addObjectCreate("classes/student/address", Address.class);
        digester.addSetProperties("classes/student/address");
        digester.addSetNext("classes/student/address","setAddress");

        Classes classes = ((Classes) digester.parse(resource));
        System.out.println(classes.toString());

    }
}
Classes{name='四年级', description='超级牛逼', students=[Student{name='张三', age=20, address=Address{streetName='武侯区', streetNumber='223'}}, Student{name='李四', age=18, address=Address{streetName='青羊区', streetNumber='900'}}]}

public class DigesterTest {

    public static void main(String[] args) throws Exception {
        URL resource = DigesterTest.class.getClassLoader().getResource("com/digester/classes.xml");

        Digester digester = new Digester();

        digester.addRuleSet(new RuleSetBase() {
            @Override
            public void addRuleInstances(Digester digester) {
                digester.addObjectCreate("classes",Classes.class);//创建一个Classes对象
                digester.addSetProperties("classes");//把classes节点全把设置到实体上
                digester.addObjectCreate("classes/student", Student.class);//创建一个Student对象
                digester.addSetProperties("classes/student");//把student节点全把设置到实体上
                digester.addSetNext("classes/student","addStudents");//把节点student,调用addStdents 添加到Classes 对象上

                digester.addObjectCreate("classes/student/address", Address.class);
                digester.addSetProperties("classes/student/address");
                digester.addSetNext("classes/student/address","setAddress");
            }
        });
        Classes classes = ((Classes) digester.parse(resource));
        System.out.println(classes.toString());

    }
}
public class DigesterTest {

    public static void main(String[] args) throws Exception {
        URL resource = DigesterTest.class.getClassLoader().getResource("com/digester/classes.xml");

        Digester digester = new Digester();

        digester.addRuleSet(new MyRuleSet());
        Classes classes = ((Classes) digester.parse(resource));
        System.out.println(classes.toString());

    }
}
//迪特米原则,知道最少原则
public class MyRuleSet extends RuleSetBase {

    @Override
    public void addRuleInstances(Digester digester) {
        digester.addObjectCreate("classes",Classes.class);//创建一个Classes对象
        digester.addSetProperties("classes");//把classes节点全把设置到实体上
        digester.addObjectCreate("classes/student", Student.class);//创建一个Student对象
        digester.addSetProperties("classes/student");//把student节点全把设置到实体上
        digester.addSetNext("classes/student","addStudents");//把节点student,调用addStdents 添加到Classes 对象上

        digester.addObjectCreate("classes/student/address", Address.class);
        digester.addSetProperties("classes/student/address");
        digester.addSetNext("classes/student/address","setAddress");
    }
}

Digester类用来将XML映射成Java类,简化XML的处理

对象创建
1.addObjectCreate(String rule,Class class)
设置节点与Java对象的映射规则,rule指定节点的筛选规则,class设置映射对象。SAX解析时,遇到rule指定的节节点,会创建一个class实例放入堆栈中。
比如:addObjectCreate(“classes”,Classes.class) 解析遇到classes节点时,会创建一个Classes实例并放入堆栈中。

属性设置
2.addSetProperties(String rule)
设置节点的属性设置规则。当解析遇到符合rule的节点时,根据属性列表中的属性值对,使用Java反射机制使用标准的JavaBean方法设置栈顶对象实例;
比如:digester.addSetProperties(“classes”),解析遇到user节点时,会获取键值对 <classes name="四年级" description="超级牛逼">获得栈顶的Classes对象,设置实例的name、description属性;

3.addBeanPropertySetter(String rule)
该方法的作用及使用方法类似于addSetProperties,只不过它是用rule所指定的标签来调用对象的setter。

父子关系管理
4.addSetNext(String rule,String methodName)
设置当前rule节点与父节点的调用规则,当遇到rule节点时,调用堆栈中的次栈顶元素调用methodName方法。将栈顶元素作为次顶元素指定方法的输入参数。

5.addCallMethod(String rule,String methodName,int paraNumber)
该方法同样设置对象的属性,但更加灵活,不需要对象具有setter
根据rule规则指定的属性,调用对象的methodName方法,paraNumber参数是表示方法需要的参数个数,当paraNumber=0时,可以单独使用,不然需要配合addCallParam方法

6.addCallParam(String rule,int paraIndex,String attributeName)
该方法与addCallMethod配合使用,根据rule指定的标签属性来调用方法
paraIndex表明需要填充的方法形参序号,从0开始,方法由addCallMethdo指定,attributeName指定标签属性名;

7。 addRuleSet(RuleSet ruleSet),添加规则集合

这个Digester,还有很多功能和方法,需要深入了解请到官网上面查询api

你可能感兴趣的:(tomcat,xml)