Digester本来仅仅是Jakarta Struts中的一个工具,用于处理struts-config.xml配置文件。 Digester 使用所需的jar包 Digester、BeanUtils、Collections、Commens-Logging 示例一: 【users.xml】 <?xml version="1.0" encoding="UTF-8"?> <users> <user username="leonard" password="123"/> <user username="leonard" password="456"/> </users> 【User.xml】 package myPack1; public class User { private String username; private String password; public String getUsername() {return username;} public void setUsername(String username) {this.username = username;} public String getPassword() {return password;} public void setPassword(String password) {this.password = password;} public String toString(){return this.username+" "+this.password;} } 【Entry.java】 File input = new File("E:/users.xml"); Digester d=new Digester(); d.setValidating(false); d.addObjectCreate("users/user", myPack1.User.class);//创建对象,并将其压 入栈Digester中顶 d.addSetProperties("users/user"); User user=(User)d.parse(input); System.out.println(user); 注意:最后返回结果为leonard 456 而不能得到一个User集合,这是因为压入栈的是User对象,最后 把该对象弹出来而已。若有需要得到一个集合,可以改进。 例二:【User.xml】【User.java】不变,多一个文件 【Users.java】 ublic class Users { public String title; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public ArrayList getAl() { return al; } public void setAl(ArrayList al) { this.al = al; } public ArrayList al=new ArrayList(); public void addUser(User u){ al.add(u); } } 【Entry.xml】 File input = new File("E:/users2.xml"); Users us=new Users(); Digester d=new Digester(); d.setValidating(false); d.push(us);//若xml文件里users标签里有熟悉将被忽略 d.addObjectCreate("users/user", myPack1.User.class); d.addSetProperties("users/user"); d.addSetNext("users/user", "addUser"); // Users user=(Users)d.parse("/users2.xml"); Users user=(Users)d.parse(input); System.out.println(((User)(user.getAl().get(1))).getUsername()); 这样一来我们压入栈的是Users,最后弹出来的还是Users。 例三: Digester.jar 中的典型例子 【foo.xml】 <?xml version="1.0" encoding="UTF-8"?> <foo name="The Parent"> <bar id="123" title="The First Child"/> <bar id="456" title="The Second Child"/> </foo> 【Bar.java】 public class Bar { private int id; private String title; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } } 【Foo.java】 import java.util.HashMap; import java.util.Iterator; public class Foo { private String name; private HashMap bars = new HashMap(); public void addBar(Bar bar) { bars.put(String.valueOf(bar.getId()), bar); } public Bar findBar(int id) { return (Bar) bars.get(String.valueOf(id)); } public void setBars(HashMap bars) { this.bars = bars; } public Iterator getBars() { return bars.keySet().iterator(); } public String getName() { return name; } public void setName(String name) { this.name = name; } } 【Entry.java】 File input = new File("E:/foo.xml"); // File input = new File("/foo.xml"); Digester digester = new Digester(); digester.setValidating(false); // Foo f=new Foo(); // digester.push(f); //完整类名定义,包名改变时需做相应变化 digester.addObjectCreate("foo", "myPack3.Foo"); digester.addSetProperties("foo"); digester.addObjectCreate("foo/bar", "myPack3.Bar"); digester.addSetProperties("foo/bar"); digester.addSetNext("foo/bar", "addBar", "myPack3.Bar"); Foo foo = (Foo) digester.parse(input); System.out.println(foo.getBars().next().getClass()); 上面的例子匹配模式和规则过于硬编码,有一种比较好的解决方案: 例四:其他文件不变,多一个规则文件rule.xml 【rule.xml】 <?xml version="1.0" encoding="UTF-8"?> <foo name="The Parent"> <bar id="123" title="The First Child"/> <bar id="456" title="The Second Child"/> </foo> 【Entry.java】 File data = new File("E:/foo.xml"); File rule = new File("E:/rule.xml"); Digester digester = DigesterLoader.createDigester(rule.toURL()); Foo foo = (Foo) digester.parse(data); System.out.println(foo.getName()); Iterator iter = foo.getBars(); while (iter.hasNext()) { System.out.println((String) iter.next()); }