http://commons.apache.org/
web service >>> xml to java object = diggester+beanutils+logging
Commons Digester 2.0 API
http://commons.apache.org/digester/
http://commons.apache.org/digester/commons-digester-2.0/docs/api/
Digester download
http://archive.apache.org/dist/commons/digester/binaries/
http://archive.apache.org/dist/commons/digester/binaries/commons-digester-2.0-bin.zip
Commons BeanUtils 1.8.2 API
http://commons.apache.org/beanutils/
http://commons.apache.org/beanutils/api/index.html
beanutils download
http://archive.apache.org/dist/commons/beanutils/binaries/
http://labs.xiaonei.com/apache-mirror/commons/beanutils/binaries/commons-beanutils-1.8.2-bin.zip
Commons Logging 1.1.2-SNAPSHOT API
http://commons.apache.org/logging/apidocs/index.html
loggind download
http://archive.apache.org/dist/commons/logging/
http://archive.apache.org/dist/commons/logging/binaries/commons-logging-1.1.1-bin.zip
webservice begin
[root@localhost digester]# pwd
/home/lindows/workspace/test/src/com/javaeye/lindows/digester
[root@localhost digester]# ll
总计 64
-rw-r--r-- 1 lindows users 835 11-25 21:17 Academy.java
-rw-r--r-- 1 lindows users 993 11-25 21:29 academyRules.xml
-rw-r--r-- 1 lindows users 984 11-20 08:18 acdemy.xml
-rw-r--r-- 1 lindows users 330 11-25 15:40 Course.java
drwxr-xr-x 2 lindows users 4096 11-25 21:31 CVS
-rw-r--r-- 1 lindows users 2114 11-25 21:23 DigestXMLJavaAcademy.java
-rw-r--r-- 1 lindows users 755 11-25 18:45 Student.java
-rw-r--r-- 1 lindows users 607 11-25 21:22 Teacher.java
http://developer.ccidnet.com/art/322/20021211/33259_1.html
相关资源
·The Jakarta Commons homepage: http://jakarta.apache.org/commons
·Two open source XML parsers suitable for use with Digester are:
o Xerces
o Crimson
·Open source projects that use Digester:
o Struts
o Tomcat
·Other Jakarta Commons components:
http://jakarta.apache.org/commons/components.html
· Erik Swenson's previous Open Source Profile column, "Reports Made Easy with JasperReports" (JavaWorld, September 2002):
http://www.javaworld.com/javaworld/jw-09-2002/jw-0920-opensourceprofile.html
· Browse the Java and XML section of JavaWorld's Topical Index:
http://www.javaworld.com/channel_content/jw-xml-index.shtml
·Chat about Java development in the JavaWorld Forum:
http://forums.devworld.com/webx?13@@.ee6b802
· Sign up for JavaWorld's free weekly email newsletters:
http://www.javaworld.com/subscribe
· You'll find a wealth of IT-related articles from our sister publications at IDG.net
Java Open Source Software: Jakarta commons Digester
http://www.itags.org/java/583235/
Hi
Anyone like to offer an example that uses the org.apache.commons.digester to create objects with a non empty constructor. Nothing too complicated - an object with a String in the Constructor will suffice.
http://www.iteye.com/topic/39330
http://philos.iteye.com/blog/176724
前段时间项目中需要根据xml文件创建java对象,而apache commons-digester是首选.
一直想把它写下来,可是总有点懒 这次介绍一下digester的常用法,其他更高级的使用方法请参阅digester doc(由于比较忙,下次有时间写一篇关于cglib的常用法,呵呵)
Apache Digester应用实例 [原创]
--------------利用 apache commons-digester 自动根据xml文件创建java 对象
<day day up>
author: binker
email: [email protected]
date: 2006/12/16
note: 本文可以任意转载,但请保持其完整性并注明来源,谢谢.
一.序
Digester是apache的一个组件 apache commons-digester.jar,通过它可以很方便的从xml文件生成java对象.你不用再象以前通过jdom或者Xerces去读取一个 document对象.(jdom和Xerces仍然有它们的用武之地及强大之处,在其它应用里你也少不了它们)
我只是用一个例子展示一下如何把xml文件生成一个java object.
首先你必须把jar加入到你的项目classpath.
(一)准备:
1.环境: Eclipse, jdk1.4+
2.需求说明:学院包含学生和老师,学生可以选修课程,老师有不同的资格认证。
3.Java Class的定义,简洁起见,略去import内容。
4.使用Digester生成java对象 必须要定义一个xml文件,此xml文件必须符合digester-rules.dtd的规则。
5.所有的xml文件必须在classpath路径中,这样可以通过Class.getClassLoader().getInputStream()读取xml文件。
(二)提要:
本示例所需的java class 和 xml文件分成四类:
1.基础xml文件: [academy.xml]: 你将要解析的xml文件.
2.基础java class: [Academy.java, Teacher.java, Student.java, Cousre.java]: 生成的对象的类定义
3.digesterRules.xml: [academyRules.xml]: 定义了基础xml文件到基础java class的匹配(映射)关系
4.Digester class: [DigestXMLJavaAcademy.java]: 根据digesterRules.xml去解析基础xml文件,并生成java 对象
(三)参考资料
1.English ebook: Pro Jakarta Commons by Harshad Oak Apress 2004
二.基础Java Class
1.学院 class Academy.java
public class Academy { private Vector students; private Vector teachers; private String name; public Academy() { students = new Vector(); teachers = new Vector(); } public void addStudent(Student student) { students.addElement(student); } public void addTeacher(Teacher teacher) { teachers.addElement(teacher); } public Vector getStudents() { return students; } public void setStudents(Vector newStudents) { students = newStudents; } public Vector getTeachers() { return teachers; } public void setTeachers(Vector newTeachers) { teachers = newTeachers; } public String getName() { return name; } public void setName(String newName) { name = newName; } }
============================================================
2.学生 Student.java
public class Student { private Vector courses; private String name; private String division; public Student() { courses = new Vector(); } public void addCourse(Course course) { courses.addElement(course); } public String getName() { return name; } public void setName(String newName) { name = newName; } public String getDivision() { return division; } public void setDivision(String newDivision) { division = newDivision; } public void setCourses(Vector courses) { this.courses = courses; } public Vector getCourses() { return courses; } }
============================================================
3.老师 Teacher.java
public class Teacher { private String name; private Vector certifications; public Teacher() { certifications = new Vector(); } public void addCertification(String certification) { certifications.addElement(certification); } public String getName() { return name; } public void setName(String newName) { name = newName; } public void setCertifications(Vector certifications) { this.certifications = certifications; } public Vector getCertifications() { return certifications; } }
============================================================
4.课程 Course.java
public class Course { private String id; private String name; public Course() { } public String getId() { return id; } public void setId(String newId) { id = newId; } public String getName() { return name; } public void setName(String newName) { name = newName; } }
三.基础XML文件
============================================================
学院 academy.xml,包含了学生(包含了课程),老师
<?xml version="1.0"?> <academy name="JAcademy" > <student name="JavaBoy" division="A"> <course> <id>C1</id> <name>JSP</name> </course> <course> <id>C2</id> <name>Servlets</name> </course> </student> <student name="JavaGirl" division="B"> <course> <id>C3</id> <name>EJB</name> </course> </student> <teacher name="JavaGuru"> <certification>SCJP</certification> <certification>SCWCD</certification> </teacher> <teacher name="JavaMaster"> <certification>OCP</certification> <certification>SCJP</certification> <certification>SCEA</certification> </teacher> </academy>
============================================================
四.Digester Class
DigestXMLJavaAcademy.java 和academyRules.xml
其中academyRules.xml用来定义xml文件和java对象的匹配(映射)关系,格式要符合digester-rules.dtd格式约束
DigestXMLJavaAcademy.java 根据academyRules.xml解析academy.xml文件并生成 Academy对象.
============================================================
1. academyRules.xml <?xml version="1.0"?> <digester-rules> <pattern value="academy"> <object-create-rule classname="com.commonsbook.chap7.academy.Academy" /> <set-properties-rule /> <pattern value="student"> <object-create-rule classname="com.commonsbook.chap7.academy.Student" /> <set-properties-rule /> <pattern value="course"> <object-create-rule classname="com.commonsbook.chap7.academy.Course" /> <bean-property-setter-rule pattern="id"/> <bean-property-setter-rule pattern="name"/> <set-next-rule methodname="addCourse" /> </pattern> <set-next-rule methodname="addStudent" /> </pattern> <pattern value="teacher"> <object-create-rule classname="com.commonsbook.chap7.academy.Teacher" /> <set-properties-rule /> <call-method-rule pattern="certification" methodname="addCertification" paramcount="1" /> <call-param-rule pattern="certification" paramnumber="0"/> <set-next-rule methodname="addTeacher" /> </pattern> </pattern> </digester-rules>
============================================================
2. DigestXMLJavaAcademy.java
public class DigestXMLJavaAcademy { public void digest(){ try { //Create Digester using rules defined in academyRules.xml Digester digester = DigesterLoader.createDigester( this.getClass().getClassLoader().getResource("academyRules.xml")); //Parse academy.xml using the Digester to get an instance of Academy Academy a = (Academy)digester.parse( this.getClass().getClassLoader().getResourceAsStream("academy.xml")); Vector vStud=a.getStudents(); Vector vTeach=a.getTeachers(); for (int i = 0; i < vStud.size(); i++) { System.out.println("Student>> "+PropertyUtils.describe(vStud.get(i))); } for (int i = 0; i < vTeach.size(); i++) { System.out.println("Teacher>> "+ PropertyUtils.describe(vTeach.get(i))); } } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { DigestXMLJavaAcademy xmlDigest= new DigestXMLJavaAcademy(); xmlDigest.digest(); } }
五.运行结果
PropertyUtils.describ(Object)用法
http://philos.iteye.com/blog/176724
使用org.apache.commons.beanutils.PropertyUtils.describ(Object)生成Map实例,其中包含了源bean中所有可读的属性。下面的代码片段演示了这个特性:
Person author = new Person(); author.setName("Timothy M. O'Brien"); Book book = new Book(); book.setName("Jakarta Commons Cookbook"); book.setAuthor(author); // 取得book中的所有可读属性,返回到map中 Map bookMap = PropertyUtils.describe(book); // Book类中包含一个Author类型的属性 // 取bookMap中的author bean,并将其所有可读属性返回到authorMap中 Map authorMap = PropertyUtils.describe(bookMap.get("author")); // 输出 // Book Name: Jakarta Commons Cookbook // Author Name: Timothy M. O'Brien System.out.println("Book Name: " + bookMap.get("name")); System.out.println("Author Name: " + authorMap.get("name"));
在上面的例子中假如Book类中还有个Integer型的page属性,代表书籍页数,在没设置的情况下,将返回null,如果page属性为int型,则将返回0。
http://commons.apache.org/fileupload/
FileUpload 1.2.1 - 18 January 2008
DWR + FileUpload 多文件上传实例
http://www.iteye.com/topic/247420
功能: 文件上传
特点: 动态显示进度, 百分比, 文件名, 文件长度, 上传速度... 剩下的自己看吧
主要技术: DWR, Apache commons FileUpload
原理: FileUpload实现上传功能, UploadListener 监听上传进度, DWR push (Reverse Ajax) 进度信息并更新页面, 实现无刷新多文件上传
运行环境: Tomcat 6, WAS 6 测试通过
WAR 包下载 见附件
顺便截个图:
http://dl.iteye.com/topics/download/53b9ba95-5a12-3d89-a545-8d7f90fddb75
end