求org.apache.commons.digester的用法 主要是如何读取、修改、保存类私有成员到XML文件 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在针对xml文件操作中,Digester提供了一种简单的方法。现在就举一个非常简单的例子,比如我们系统通过一个xml文件来创建一个类的实例。假设我们现在有这样一个xml文件,其中包含了2个学生的信息。xml文件内容如下:Java BoyJSPJava GirlEJB接着我们构建一个学生类,这个类非常简单,分别包括了name和course。package demo.commons;public class Student {private String name;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);}}接着我们利用Digester来构造一个DigestStutdents的java类,用于能创建多个学生的实例。package demo.commons;import java.util.Vector;import org.apache.commons.digester.Digester;public class DigestStudents {Vector students;public DigestStudents() {students= 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 stackdigester.push(this);//Creates a new instance of the Student classdigester.addObjectCreate("students/student", Student.class);//Uses setName method of the Student instance//Uses tag name as the property namedigester.addBeanPropertySetter("students/student/name");//Uses setCourse method of the Student instance//Explicitly specify property name as 'course'digester.addBeanPropertySetter("students/student/course", "course");//Move to next studentdigester.addSetNext("students/student", "addStudent");DigestStudents ds = (DigestStudents) digester.parse(this.getClass().getClassLoader().getResourceAsStream("students.xml"));//Print the contents of the VectorSystem.out.println("Students Vector "+ds.students);} catch (Exception ex) {ex.printStackTrace();}}public void addStudent(Student stud) {//Add a new Student instance to the Vectorstudents.add(stud);}}当上述代码被构建后,就能获得一个 students的对象,该对象的内容就是 [Name=Java Boy Course=JSP, Name=Java Girl Course=EJB]。 org.apache.commons.digester软件包能方便地将XML文档所定义的元素转化为JAVA对象,其实它的用法有点象栈(当然内在的原理就是那个古老的东西,只是提供了更高一层的封装)。//生成一个digester。主要需要引进commons-logging.jar、commons-collections.jar、commons-beanutils.jarDigesterdigester=newDigester();//设置对XML文档资料是否进行DTD验证digester.setValidating(false);//当遇见catalog元素的时候,产生一个Catalog对象digester.addObjectCreate("catalog",Catalog.class);//当遇见catalog元素下面的book的时候,产生一个Book对象digester.addObjectCreate("catalog/book",Book.class);//当遇见catalog元素下面的book的author时候,调用author属性的Set方法digester.addBeanPropertySetter("catalog/book/author","author");digester.addBeanPropertySetter("catalog/book/title","title");//当再一次遇见catalog元素下面的book的时候,调用catalog类的addBook()方法digester.addSetNext("catalog/book","addBook");digester.addObjectCreate("catalog/magazine",Magazine.class);digester.addBeanPropertySetter("catalog/magazine/name","name");digester.addObjectCreate("catalog/magazine/article",Article.class);//addSetProperties()是将对应元素的属性赋值。digester.addSetProperties("catalog/magazine/article","page","page");digester.addBeanPropertySetter("catalog/magazine/article/headline");digester.addSetNext("catalog/magazine/article","addArticle");digester.addSetNext("catalog/magazine","addMagazine");//"F:\\Digester\\catalog.xml"为XML文档Fileinput=newFile("F:\\Digester\\catalog.xml");Catalogc=(Catalog)digester.parse(input);System.out.println(c.toString());在测试以上网站的例子的时候引进:commons-digester.jar,另外请你引进:commons-logging.jar、commons-collections.jar、commons-beanutils.jar这是因为在执行:Digesterdigester=newDigester();的时候配置了LOG(在apache下面的很多组件都会用到它的公用组件)。 有问题在加我讨论 [email protected] 非常感谢btb386大虾的回答。要是我生成的对象私有成员被改变了值,然后我想保存回xml文件,请问该怎么做呢? http://www.matrix.org.cn/resource/article/44/44062_Java+Annotation+Apt.html这个上面讲的很清楚的。 关于Java Static修饰的一个怪异问题 提交后,URL地址,如何去掉.do呢? 线程运行效率问题 应界毕业生,对于java的困惑,定散分 一个小问题,顶者有分 帮我好吗?在线等。。 我在SUN网站下载了SDK1.4,是BIN,但不会在LINUX下安装,谁来帮我啊. jbuilder6 来了!!!!!!! 我现在用JAVA做了一个应用程序,但我不会打包,请告诉我打包程序的下载地址,最好有使用方法??? HttpClient 某一时刻会read time out 问个简单的写TXT文件的问题 (请教大家)关于数据库连接问题
xml文件内容如下:Java Boy
JSP
Java Girl
EJB接着我们构建一个学生类,这个类非常简单,分别包括了name和course。package demo.commons;public class Student {
private String name;
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);
}
}
接着我们利用Digester来构造一个DigestStutdents的java类,用于能创建多个学生的实例。package demo.commons;import java.util.Vector;
import org.apache.commons.digester.Digester;public class DigestStudents {
Vector students;public DigestStudents() {
students= 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.push(this);//Creates a new instance of the Student class
digester.addObjectCreate("students/student", Student.class);//Uses setName method of the Student instance
//Uses tag name as the property name
digester.addBeanPropertySetter("students/student/name");//Uses setCourse method of the Student instance
//Explicitly specify property name as 'course'
digester.addBeanPropertySetter("students/student/course", "course");//Move to next student
digester.addSetNext("students/student", "addStudent");DigestStudents ds = (DigestStudents) digester.parse(this.getClass()
.getClassLoader()
.getResourceAsStream("students.xml"));//Print the contents of the Vector
System.out.println("Students Vector "+ds.students);
} catch (Exception ex) {
ex.printStackTrace();
}
}public void addStudent(Student stud) {
//Add a new Student instance to the Vector
students.add(stud);
}
}当上述代码被构建后,就能获得一个 students的对象,该对象的内容就是 [Name=Java Boy Course=JSP, Name=Java Girl Course=EJB]。
Digesterdigester=newDigester();//设置对XML文档资料是否进行DTD验证
digester.setValidating(false);//当遇见catalog元素的时候,产生一个Catalog对象
digester.addObjectCreate("catalog",Catalog.class);//当遇见catalog元素下面的book的时候,产生一个Book对象
digester.addObjectCreate("catalog/book",Book.class);
//当遇见catalog元素下面的book的author时候,调用author属性的Set方法
digester.addBeanPropertySetter("catalog/book/author","author");
digester.addBeanPropertySetter("catalog/book/title","title");
//当再一次遇见catalog元素下面的book的时候,调用catalog类的addBook()方法
digester.addSetNext("catalog/book","addBook");digester.addObjectCreate("catalog/magazine",Magazine.class);
digester.addBeanPropertySetter("catalog/magazine/name","name");digester.addObjectCreate("catalog/magazine/article",Article.class);
//addSetProperties()是将对应元素的属性赋值。
digester.addSetProperties("catalog/magazine/article","page","page");
digester.addBeanPropertySetter("catalog/magazine/article/headline");
digester.addSetNext("catalog/magazine/article","addArticle");digester.addSetNext("catalog/magazine","addMagazine");
//"F:\\Digester\\catalog.xml"为XML文档
Fileinput=newFile("F:\\Digester\\catalog.xml");
Catalogc=(Catalog)digester.parse(input);
System.out.println(c.toString());
在测试以上网站的例子的时候引进:commons-digester.jar,另外请你引进:commons-logging.jar、commons-collections.jar、commons-beanutils.jar
这是因为在执行:Digesterdigester=newDigester();的时候配置了LOG(在apache下面的很多组件都会用到它的公用组件)。
这个上面讲的很清楚的。