参考http://search.csdn.net/Expert/topic/1369/1369055.xml?temp=.470791
写出两个读取例子
/***********/
/*
* 创建日期 2005-3-8
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package com.yourcompany.struts;
/**
* @author wangshangting
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
import org.apache.commons.digester.*;
import java.io.*;
public class DigesterDriver {
public static void main( String[] args ) {
try {
Digester digester = new Digester();
digester.setValidating( false );
digester.addObjectCreate( "catalog", Catalog.class );
digester.addObjectCreate( "catalog/book", Book.class );
digester.addBeanPropertySetter( "catalog/book/author", "author" );
digester.addBeanPropertySetter( "catalog/book/title", "title" );
digester.addSetNext( "catalog/book", "addBook" );
digester.addObjectCreate( "catalog/magazine", Magazine.class );
digester.addBeanPropertySetter( "catalog/magazine/name", "name" );
digester.addObjectCreate( "catalog/magazine/article", Article.class );
digester.addSetProperties( "catalog/magazine/article", "page", "page" );
digester.addBeanPropertySetter( "catalog/magazine/article/headline" );
digester.addSetNext( "catalog/magazine/article", "addArticle" );
digester.addSetNext( "catalog/magazine", "addMagazine" );
Catalog c = (Catalog)digester.parse( "WebRoot\\WEB-INF\\catalog.xml" );
System.out.println( c.toString() );
c.getTitle("Author 2");
c.getheadline("Mag Title 2","17");
} catch( Exception exc ) {
exc.printStackTrace();
}
}
}
/************/
package com.yourcompany.struts;
import java.util.Vector;
public class Catalog {
private Vector books;
private Vector magazines;
public Catalog() {
books = new Vector();
magazines = new Vector();
}
public void addBook( Book rhs ) {
books.addElement( rhs );
}
public void addMagazine( Magazine rhs ) {
magazines.addElement( rhs );
}
public String toString() {
String newline = System.getProperty( "line.separator" );
StringBuffer buf = new StringBuffer();
buf.append( "--- Books ---" ).append( newline );
for( int i=0; i<books.size(); i++ ){
buf.append( books.elementAt(i) ).append( newline );
}
buf.append( "--- Magazines ---" ).append( newline );
for( int i=0; i<magazines.size(); i++ ){
buf.append( magazines.elementAt(i) ).append( newline );
}
return buf.toString();
}
/*通过author找title
* @String author
*/
public void getTitle(String author)
{
Book b=null;
for(int i=0;i<books.size();i++)
{
b=(Book)books.elementAt(i);
if(b.getAuthor().equals(author))
{
System.out.println(b.getTitle());
}
}
}
/*通过Magazine Name,page找headline
* @String MagaName,String page
*/
public void getheadline(String MagaName,String page)
{
Magazine m=null;
for (int i=0;i<magazines.size();i++)
{
m=(Magazine)magazines.elementAt(i);
if(MagaName.equals(m.getName()))
m.getHeadline(page);
}
}
}
/****************/
package com.yourcompany.struts;
import java.util.Vector;
public class Magazine {
private String name;
private Vector articles;
public Magazine() {
articles = new Vector();
}
public String getName()
{
return name;
}
public void setName( String rhs ) { name = rhs; }
public void addArticle( Article a ) {
articles.addElement( a );
}
public String toString() {
StringBuffer buf = new StringBuffer( "Magazine: Name='" + name + "' ");
for( int i=0; i<articles.size(); i++ ){
buf.append( articles.elementAt(i).toString() );
}
return buf.toString();
}
/*通过page找headline
* @String page
*/
public void getHeadline(String page)
{
Article a=null;
for(int k=0;k<articles.size();k++)
{
a=(Article)articles.elementAt(k);
if(a.getPage().equals(page))
{
System.out.println(a.getHeadline());
}
}
}
}