分类:
2008-07-28 21:01:35
===1.前期准备====
用digester前先准备好它所需要的jar包,共用4个,分别为:commons-collections-3.2.jar,commons-beanutils-core-1.8.0-BETA.jar,commons-digester-1.8.jar,commons-logging-1.1.1.jar,这里是他们的.
===2.具体用法====
下面以我做的用xml解析Article为例解释他们的用法:
源码片段如下:
Article articles = new Articles(); Digester digester = new Digester(); digester.push(articles); // 将articles压入堆栈中 digester.setValidating(false);// 不使用验证策略 digester.addSetProperties("articles");// 对articles属性进行赋值 digester.addObjectCreate("articles/article", com.leadal.training.xml.hdc.model.Article.class);// 当遇到articles/article //(它们为父子节点),生成Article对象 digester.addCallMethod("articles/article/id", "setId", 1);// // 当遇到articles/article/id,是使用Article的setId对象,对Article的id进行赋值, // 0代表按xml对象赋值,1代表一个参数,当属性类型不为String时要指明参数个数,否则会报错 digester.addCallMethod("articles/article/author", "setAuthor", 0);// 同上 digester.addCallMethod("articles/article/title", "setTitle", 0); digester.addCallMethod("articles/article/createdTime", "setCreatedTime", 0); digester.addCallMethod("articles/article/content", "setContent", 0); digester.addCallMethod("articles/article/catalogId", "setCatalogId", 1); digester.addCallMethod("articles/article/state", "setState", 0); digester.addSetNext("articles/article", "addArticle", "com.leadal.training.xml.hdc.model.Article"); // digester.addSetNestProperties("articles/article"); digester.addSetNestedProperties("articles/article");//自动为articles/article下的所以属性赋值,有了 //这一行上面的分别给每个变量赋值的语句可以删除
在使用前应该做好Article.java和Articles.java两个类,Article.java为了保存每个对象,Articles.java 为了保存所以的Article对象,这两个类的具体代码请看系统中的源码。另外还要注意的就是在指定路径时要这样声明:
ParseXmlByDigester parseXml = new ParseXmlByDigester(); parseXml.parse("file:\\D:\\Article.xml");// digester的文件指定格式,以file开头
也就是在路径前要加入file前缀,这和spring中解析文件资源的用法类似可以参考它。
3.帮助文档
要获得更加具体的资源可以参照digester的帮助文档,这是它的要获得更加具体的资源可以参照digester的帮助文档,这是它的
4.下面是我用到的文档。希望后来的人可以在用digester时少走弯路。
Article.xml
*****************************************************************************************************************************
Articles.java
public class Articles implements Serializable {
private static final long serialVersionUID = 4616751134472546310L;
// HashMap
public LinkedList
public Articles() {
}
public void addArticle(Article article) {
articleList.addLast(article);
}
}
}
*****************************************************************************************************************************
Article 就是普通的java bean属性和xml的元素相同,在此省略。
*****************************************************************************************************************************
import org.apache.commons.digester.Digester;
import com.leadal.training.xml.hdc.model.Articles;
/**
* 解析xml,digester法
*
* @author HuangDaCang
*
*/
public class ParseXmlByDigester {
public Articles articles;
public void parse(String sFileName) {
articles = new Articles();
Digester digester = new Digester();
// Articles articles = new Articles();
digester.push(articles); // 将articles压入堆栈中
digester.setValidating(false);// 不使用验证策略
digester.addSetProperties("articles");// 对articles属性进行赋值
digester.addObjectCreate("articles/article",
com.leadal.training.xml.hdc.model.Article.class);// 当遇到articles/article(它们为父子节点),生成Article对象
digester.addCallMethod("articles/article/id", "setId", 1);//
// 当遇到articles/article/id,是使用Article的setId对象,对Article的id进行赋值,
// 0代表按xml对象赋值,1代表一个参数
digester.addCallMethod("articles/article/author", "setAuthor", 0);// 同上
digester.addCallMethod("articles/article/title", "setTitle", 0);
digester.addCallMethod("articles/article/createdTime",
"setCreatedTime", 0);
digester.addCallMethod("articles/article/content", "setContent", 0);
digester.addCallMethod("articles/article/catalogId", "setCatalogId", 1);
digester.addCallMethod("articles/article/state", "setState", 0);
digester.addSetNext("articles/article", "addArticle",
"com.leadal.training.xml.hdc.model.Article");
// digester.addSetNestProperties("articles/article");
digester.addSetNestedProperties("articles/article");
try {
articles = (Articles) digester.parse(sFileName);// 用digester解析指定的文件
} catch (java.io.IOException ioe) {
System.out.println("IOException reading input file:"
+ ioe.getMessage());
System.exit(-1); // 遇到异常非正常退出系统
} catch (org.xml.sax.SAXException se) {
System.out.println("SAXException Error parsing input file:"
+ se.getMessage());
System.exit(-1);
} catch (Exception ex) {
System.out.println("Exception parse error!" + ex.getMessage());
}
}
}
*****************************************************************************************************************************