正好看到,给个例子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 method s 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...
}
}在上面的SampleDigester类中,每当匹配到 'datasources/datasource'时都会自动调用addDataSource()方法。addCallParam()方法将匹配到的数据项内容作为传入addDataSource()方法的参数。在addDataSource()方法中,你就可以创建具体的DataSouce并把它添加入你的DataSource集合中去。
{
public void run() throws IOException, SAXException
{
Digester digester = new Digester(); // This method pushes this (SampleDigester) class to the Digesters
// object stack making its method s 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...
}
}在上面的SampleDigester类中,每当匹配到 'datasources/datasource'时都会自动调用addDataSource()方法。addCallParam()方法将匹配到的数据项内容作为传入addDataSource()方法的参数。在addDataSource()方法中,你就可以创建具体的DataSouce并把它添加入你的DataSource集合中去。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货