Apache Commons Digester 使用实例

 
Many projects read XML configuration files to provide initialization of various Java objects within the system. There are several ways of doing this, and the Digester component was designed to provide a common implementation that can be used in many different projects.
 
Digester 不是一个 XML Parser ,它只是对 SAX 更高层次上的一个封装使用 Digester,将XML映射成javaBean. 我们无须了解SAX和DOM的解析过程,只要给Digester添加一些解析规则,就能对一个xml文件进行解析。Digester使用堆栈来保存xml节点(stack.push()方法),当该xml节点中嵌套的所有子节点解析完毕,该节点将被弹出(stack.pup()方法)
Digester最大的优点就是使用模式匹配来表示xml文件中节点之间的父子关系

实例代码如下:

 

package  digester;
public   class  Student  {
    
private String name;
    
private String from;
    
private String course;
    
public Student() {
    }

    
public String getName() {
        
return name;
    }

    
public void setName(String newName) {
        name 
= newName;
    }

    
public String getCourse() {
        
return course;
    }

    
public void setCourse(String newCourse) {
        course 
= newCourse;
    }

    
public String toString() {
        
return("Name="+this.name + " & Course=" +  this.course+" & from="+from);
    }

    
public String getFrom() {
        
return from;
    }

    
public void setFrom(String from) {
        
this.from = from;
    }

}

 

 

package  digester;

import  java.util.Vector;

public   class  StuClass  {
    
private String name;
    
private Vector students = new Vector();
    
public String getName() {
        
return name;
    }

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

    
public Vector getStudents() {
        
return students;
    }

    
public void setStudents(Vector students) {
        
this.students = students;
    }

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

}

 

<? xml version = " 1.0 "  encoding = " UTF-8 "   ?>

< stuClass  name = " fddfdf " >
        
< student >
                
< name  from = " cn " > Java Boy </ name >
                
< course > JSP </ course >
        
</ student >
        
< student >
                
< name > Java Girl </ name >
                
< course > EJB </ course >
        
</ student >
</ stuClass >


package  digester;

import  java.util.Vector;

import  org.apache.commons.digester.Digester;

public   class  DigestStudents  {
    Vector stuClass;
    
public DigestStudents() {
        stuClass
= new Vector();
    }

    
public static void main(String[] args) {
        DigestStudents digestStudents 
= new DigestStudents();
        digestStudents.digest();
    }

    
private void digest() {
        
try {
            Digester digester 
= new Digester();
            
//Push the current object onto the stack
            digester.setValidating(false);
            
//Creates a new instance of the Student class
            digester.addObjectCreate("stuClass""digester.StuClass");
            digester.addSetProperties(
"stuClass","name","name");
            digester.addObjectCreate( 
"stuClass/student""digester.Student" );
            
//Uses setName method of the Student instance
            
//Uses tag name as the property name
            
//addCallMethod与addBeanPropertySetter等价
            
// 参数 0代表一个参数,默认就是当前读的数据,最后一个参数0表示参数个数
            digester.addCallMethod("stuClass/student/name""setName",0);
            digester.addSetProperties(
"stuClass/student/name""from","from");
            
//加上一个属性名form
            
//digester.addBeanPropertySetter( "stuClass/student/name");
            
//Uses setCourse method of the Student instance
            
//Explicitly specify property name as 'course'
            digester.addBeanPropertySetter( "stuClass/student/course");
            
//Move to next student,addStudent为其中的一个方法
            digester.addSetNext( "stuClass/student""addStudent","digester.Student" );
            StuClass ds 
= (StuClass) digester.parse(this.getClass()
                                .getClassLoader()
                                .getResourceAsStream(
"digester/students.xml"));
            
//Print the contents of the Vector
            System.out.println("Students Vector "+ds.getName());
            System.out.println(
"Students Vector "+ds.getStudents());
        }
 catch (Exception ex) {
            ex.printStackTrace();
        }

    }

//    public void addStudent( Student stud ) {
//        //Add a new Student instance to the Vector
//        stuClass.add( stud );
//    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值