Person.java类:
import java.io.Serializable; public class Person implements Serializable { public String id; public String name; public int age; public Person() { } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
data.xml文件如下:
TestDigester1.java类:
import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.List; import org.apache.commons.digester3.Digester; import org.apache.log4j.Logger; import org.xml.sax.SAXException; import com.commonsdigester.dto.Person; public class TestDigester1 { static Logger logger = Logger.getLogger(TestDigester.class); /** * @param args */ public static void main(String[] args) { Digester digester = new Digester(); //此方法作用是是否对XML文档进行DTD验证 digester.setValidating(false); //addObjectCreate的作用是:当遇见people元素的时候,产生一个java.util.ArrayList对象 digester.addObjectCreate("people", "java.util.ArrayList"); //addSetProperties作用是:对people元素所含的属性赋值(即下面people/person中的对象值) digester.addSetProperties("people"); //当遇见people/person元素的时候,产生一个com.commonsdigester.dto.Person对象 digester.addObjectCreate("people/person", "com.commonsdigester.dto.Person"); //对people/person元素所含的属性赋值给Person对象 digester.addSetProperties("people/person"); //当再一次遇见 people下的person元素的时候,调用ArrayList类的add()方法,add:com.commonsdigester.dto.Person对象 digester.addSetNext("people/person", "add", "com.commonsdigester.dto.Person"); try { InputStream inputdata = new FileInputStream("D:\\Workspaces\\TestJavaStudy\\src\\com\\commonsdigester\\dto\\data.xml"); List personList = (List) digester.parse(inputdata); for (int i = 0; i < personList.size(); i++) { Person person = (Person) personList.get(i); System.out.println(person.getId() + " -- " + person.getName() + " -- " + person.getAge()); } } catch (IOException e) { logger.error("load People.xml error.", e); } catch (SAXException e) { logger.error("load People.xml error.", e); } } }
第二种xml格式的解析:
Person.xml文件如下:
Shibo Yu 28 Shihuan 45 Old Ages 53
TestDigester2.java类:
import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.List; import org.apache.commons.digester3.Digester; import org.apache.log4j.Logger; import org.xml.sax.SAXException; import com.commonsdigester.dto.Person; public class TestDigester2 { static Logger logger = Logger.getLogger(TestDigester.class); /** * @param args */ public static void main(String[] args) { Digester digester = new Digester(); //此方法作用是是否对XML文档进行DTD验证 digester.setValidating(false); //addObjectCreate的作用是:当遇见people元素的时候,产生一个java.util.ArrayList对象 digester.addObjectCreate("people", "java.util.ArrayList"); //addSetProperties作用是:对people元素所含的属性赋值(即下面people/person中的对象值) digester.addSetProperties("people"); //当遇见people/person元素的时候,产生一个com.commonsdigester.dto.Person对象 digester.addObjectCreate("people/person", "com.commonsdigester.dto.Person"); //对people/person元素所含的属性赋值给Person对象 digester.addBeanPropertySetter("people/person/name"); digester.addBeanPropertySetter("people/person/age"); digester.addSetProperties("people/person"); //当再一次遇见 people下的person元素的时候,调用ArrayList类的add()方法,add:com.commonsdigester.dto.Person对象 digester.addSetNext("people/person", "add", "com.commonsdigester.dto.Person"); try { // 解析XML文档 InputStream input = new FileInputStream("D:\\Workspaces\\TestJavaStudy\\src\\com\\commonsdigester\\dto\\Person.xml"); List people = (List) digester.parse(input); for (int i = 0; i < people.size(); i++) { Person person = (Person) people.get(i); System.out.println(person.getId() + " -- " + person.getName() + " -- " + person.getAge()); } } catch (IOException e) { logger.error("load People.xml error.", e); } catch (SAXException e) { logger.error("load People.xml error.", e); } } }
【注】第二种解析中关注digester. addBeanPropertySetter(" people/person/name");
digester. addBeanPropertySetter(" people/person/age");这两句。
addBeanPropertySetter方法还有其它参数形式,详情可参考官方API文档。