Digester digester = new Digester();
//新建类
digester.setValidating(false);
//设置不用DTD验证
digester.addObjectCreate("user", addressbook.model.UserBean.class);
//创建对象
digester.addSetProperties("user" ,"userName","userName");
//设置属性关联性
digester.addBeanPropertySetter("user/password","password");
//设置值相关联
digester.addSetNext("user/userName", "setUserName");
//不同层次嵌套
ub=(UserBean)digester.parse(srcfile);
//取出关联后的结果
addressbook.model.UserBean user =new addressbook.model.UserBean();
digester.push(user);
digester.addObjectCreate("user", addressbook.model.UserBean.class);
//push ,addObjectCreate一样的效果.但是也是有区别的,push只能建立一个对象,addobjectCreate是每次都建立对象,所以在有多个时要用后者,
digester.addCallMethod(
digester.addBeanPropertySetter("user/password","password");
//addCallMethod与addBeanPropertySetter等价
// 参数 0代表一个参数,默认就是当前读的数据
Digester 遍历整个 DOM 树, 当遇到一个元素时,便找到与该元素路径匹配的 Rule,并调用这个 Rule 来处理该元素。 Rule 使用 Digester 的 Object Stack 来使用或存放中间处理过程产生的对象。当整棵数遍历完毕时,Object Stack 栈底的对象即为最后结果对象。
<?xml version="1.0"?>
<datasources>
<datasource>
<name>HsqlDataSource</name>
<driver>org.hsqldb.jdbcDriver</driver>
<url>jdbc:hsqldb:hsql://localhost</url>
<username>sa</username>
<password></password>
</datasource>
<datasource>
<name>OracleDataSource</name>
<driver>oracle.jdbc.driver.OracleDriver</driver>
<url>jdbc:oracle:thin:@localhost:1521:orcl</url>
<username>scott</username>
<password>tiger</password>
</datasource>
</datasources>
To use Digester you must create an instance of the Digester class, push any required objects to the Digester's object stack, add a set of processing rules, and finally parse the file. Here is an example:
Digester digester = new Digester();
digester.addObjectCreate("datasources/datasource", "DataSource");
digester.addCallMethod("datasources/datasource/name","setName",0);
digester.addCallMethod("datasources/datasource/driver","setDriver", 0);
digester.parse("datasource.xml");
In this example, the addObjectCreate() method adds an ObjectCreateRule to the 'datasources/datasource' pattern. The ObjectCreateRule creates a new instance of the DataSource class and pushes the instance to the Digester's object stack. Next, addCallMethod() adds a CallMethodRule to two patterns. The CallMethodRule calls the specified method of the object at the top of the object stack. The last addCallMethod() argument is the number of additional parameters to be passed into the method. Since the number is zero, the matching element's body passes to the method.
If this code runs against our sample XML file, here's what happens:
A new instance of the DataSource class is created and pushed to the object stack
The setName(String name) method of the newly instantiated DataSource object is called with the argument 'HsqlDataSource'
The setDriver(String driver) method of the newly instantiated DataSource object is called with the argument 'OracleDataSource'
At the end of the 'datasource' element, the object pops off the stack, and the process repeats itself
The problem with this example is that the ObjectCreateRule pops off the object it creates when its associated element completes. When Digester finishes parsing the document, only the last object created remains. Solve this problem by pushing an object to the stack before parsing begins, and then call that object's methods to create any objects you need. The following class provides an example of this:
public class SampleDigester
{
public void run() throws IOException, SAXException
{
Digester digester = new Digester();
// This method pushes this (SampleDigester) class to the Digesters
// object stack making its methods available to processing rules.
digester.push(this);
// This set of rules calls the addDataSource method and passes
// in five parameters to the method.
digester.addCallMethod("datasources/datasource", "addDataSource", 5);
digester.addCallParam("datasources/datasource/name", 0);
digester.addCallParam("datasources/datasource/driver", 1);
digester.addCallParam("datasources/datasource/url", 2);
digester.addCallParam("datasources/datasource/username", 3);
digester.addCallParam("datasources/datasource/password", 4);
// This method starts the parsing of the document.
digester.parse("datasource.xml");
}
// Example method called by Digester.
public void addDataSource(String name,
String driver,
String url,
String userName,
String password)
{
// create DataSource and add to collection...
}
}
In the SampleDigester class, the addDataSource() method is called each time the pattern 'datasources/datasource' is matched. The addCallParam() methods add CallParamRules that pass the matching elements' bodies as addDataSource() method parameters. In the addDataSource() method, you create the actual DataSource and add it to your collection of DataSources.
使用自定义的规则
对于第一个例子,不使用内建的规则,而使用自定的规则处理,用于演示自定义规
则的用法
public class ListRule
extends Rule {
public void begin(String namespace, String name, Attributes attributes)
throws Exception {
/*将一个新的 ArrayList 对象放入 Object Stack 中*/
digester.push(new ArrayList()); } }
public class BeanRule
extends Rule {
public void begin(String namespace, String name, Attributes attributes)
throws Exception {
/*取出栈顶的对象,当处理该规则时,栈顶应该是 ArrayList*/
ArrayList list = (ArrayList) digester.peek();
Bean bean = new Bean();
bean.setId(attributes.getValue("id"));
bean.setDescription(attributes.getValue("description"));
list.add(bean); } }
Digester digester = new Digester();
digester.addRule("list", new ListRule()):
digester.addRule("list/bean" new BeanRule());
/*TODO: Load xml file as stream*/
InputStream is = null;
ArrayList list = (ArrayList) digester.parse(is);
;