现在的位置: 首页 > 综合 > 正文

Apache Commons Digester 使用实例

2013年08月06日 ⁄ 综合 ⁄ 共 4124字 ⁄ 字号 评论关闭
 

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


抱歉!评论已关闭.