Digester

test.xml
<?xml version='1.0'?>
<address-book>
  <person id="1" category="acquaintance">
    <name>Gonzo</name>
    <email type="email1">gonzo@email1</email>
    <email type="email2">gonzo@email2</email>
    <address>
      <type>home</type>
      <street>123 Maine Ave.</street>
      <city>Las Vegas</city>
      <state>NV</state>
      <zip>01234</zip>
      <country>USA</country>
    </address>
    <address>
      <type>business</type>
      <street>234 Maple Dr.</street>
      <city>Los Angeles</city>
      <state>CA</state>
      <zip>98765</zip>
      <country>USA</country>
    </address>
  </person>
</address-book>

-----------------------------------
 public static void main(String[] args) {
       
        String filename = "E:\\workspace\\Test\\test.xml";
        Digester d = new Digester();
      
        MyAddressBook book = new MyAddressBook();
        d.push(book);
       
        addRules(d);
        try {
            java.io.File srcfile = new java.io.File(filename);
            d.parse(srcfile);
        }
        catch(java.io.IOException ioe) {
            System.out.println(ioe.getMessage());
        }
        catch(org.xml.sax.SAXException se) {
            System.out.println(se.getMessage());
        }
        book.print();
    }
    
    private static void addRules(Digester d) {
    	//创建person对象
        d.addObjectCreate("address-book/person", Person.class);
        // addSetProperties方法是将XML<person id="1" category="acquaintance">属性直接赋给person
        d.addSetProperties("address-book/person");
        
        d.addSetNext("address-book/person", "addPerson");
        // 0表示该方法有一个参数,就是 <name>Gonzo</name>标记中的文本值
        d.addCallMethod("address-book/person/name", "setName", 0);
        // 2表示有两个参数  <email type="email1">gonzo@email1</email>
        d.addCallMethod("address-book/person/email", "addEmail", 2);
        // 0, "type"表示第一参数值为属性type标记内容,1,没有说明默认指的是标记中的文本,即gonzo@email1
        d.addCallParam("address-book/person/email", 0, "type");
        d.addCallParam("address-book/person/email", 1);
      
        d.addObjectCreate("address-book/person/address", Address.class);
        d.addSetNext("address-book/person/address", "addAddress");
        /*说明addSetNestedProperties 将xm中address-book/person/address的直接全部赋给java对象
         *标签名和address类属性名一致时才可以
         * */
       d.addSetNestedProperties("address-book/person/address");
        
        /**
         * 当xml中
         * <address type="business" street="2" city="3" state="4" zip="5" country="6"/>
         *  可以用以下方式,但是属性标签必须和类属性名称一致,否则属性对应值为null
         *  d.addSetProperties("address-book/person/address");
         * **/
     
    }

对应的bean如下,应用时加上get,set方法
public class Person {
  private int id;
  private String name;
  private String category;
  private HashMap emails = new HashMap();
  private List addresses = new ArrayList();
}

public class Address {
    private String type;
    private String street;
    private String city;
    private String state;
    private String zip;
    private String country;
}

public class MyAddressBook {

	public List<Person> alist = new ArrayList<Person>();
	
	public void addPerson(Person p)
	{
		alist.add(p);
	}
}

你可能感兴趣的:(DI)