Context context = new StandardContext(); Loader loader = new WebappLoader(); context.setLoader(loader);就完成了向context容器裡添加WepappLoader的功能。
在這個xml裡面,根元素是employee,裡面包含一個元素office,office裡面又包含一個元素address。
employee是類名,程序會按照上面方法的第三個參數去employee元素裡找對應的屬性值作為要加載的類,當然如果找不到那個屬性的話,就是有上面方法的第二個參數來加載。同樣第二個參數可以使String類型的類名也可以使Class。
(employee,office,address都在文章的最後)
package ex15.pyrmont.digestertest; import java.io.File; import java.util.ArrayList; import java.util.Iterator; import org.apache.commons.digester.Digester; public class Test02 { public static void main(String[] args) { String path = System.getProperty("user.dir") + File.separator + "src"+File.separator + "etc"; File file = new File(path, "employee2.xml"); Digester digester = new Digester(); // add rules digester.addObjectCreate("employee", "ex15.pyrmont.digestertest.Employee"); digester.addSetProperties("employee"); digester.addCallMethod("employee", "printName"); digester.addObjectCreate("employee/office", "ex15.pyrmont.digestertest.Office"); digester.addSetProperties("employee/office"); digester.addSetNext("employee/office", "addOffice"); digester.addObjectCreate("employee/office/address", "ex15.pyrmont.digestertest.Address"); digester.addSetProperties("employee/office/address"); digester.addSetNext("employee/office/address", "setAddress"); try { Employee employee = (Employee) digester.parse(file); ArrayList> offices = employee.getOffices(); Iterator> iterator = offices.iterator(); System.out.println("-------------------------------------------------"); while (iterator.hasNext()) { Office office = (Office) iterator.next(); Address address = office.getAddress(); System.out.println(office.getDescription()); System.out.println("Address : " + address.getStreetNumber() + " " + address.getStreetName()); System.out.println("--------------------------------"); } } catch(Exception e) { e.printStackTrace(); } } }
至於最後的運行結果大家自己猜一下,然後運行一下看看。
package ex15.pyrmont.digestertest; import java.io.File; import java.util.ArrayList; import java.util.Iterator; import org.apache.commons.digester.Digester; public class Test03 { public static void main(String[] args) { String path = System.getProperty("user.dir") + File.separator + "src"+File.separator + "etc"; File file = new File(path, "employee2.xml"); Digester digester = new Digester(); digester.addRuleSet(new EmployeeRuleSet()); //EmployeeRuleSet是什麼東西? try { Employee employee = (Employee) digester.parse(file); ...... 與Test2一樣 } catch(Exception e) { e.printStackTrace(); } } }
package ex15.pyrmont.digestertest; import org.apache.commons.digester.Digester; import org.apache.commons.digester.RuleSetBase; //一定要繼承RuleSetBase public class EmployeeRuleSet extends RuleSetBase { //復寫 addRuleInstances public void addRuleInstances(Digester digester) { // add rules digester.addObjectCreate("employee", "ex15.pyrmont.digestertest.Employee"); digester.addSetProperties("employee"); digester.addObjectCreate("employee/office", "ex15.pyrmont.digestertest.Office"); digester.addSetProperties("employee/office"); digester.addSetNext("employee/office", "addOffice"); digester.addObjectCreate("employee/office/address","ex15.pyrmont.digestertest.Address"); digester.addSetProperties("employee/office/address"); digester.addSetNext("employee/office/address", "setAddress"); } }
package ex15.pyrmont.digestertest; import java.util.ArrayList; public class Employee { private String firstName; private String lastName; private ArrayListoffices = new ArrayList (); public Employee() { System.out.println("Creating Employee"); } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { System.out.println("Setting firstName : " + firstName); this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { System.out.println("Setting lastName : " + lastName); this.lastName = lastName; } public void addOffice(Office office) { System.out.println("Adding Office to this employee"); offices.add(office); } public ArrayList getOffices() { return offices; } public void printName() { System.out.println("My name is " + firstName + " " + lastName+"sssssssss"); } } package ex15.pyrmont.digestertest; public class Office { private Address address; private String description; public Office() { System.out.println("..Creating Office"); } public String getDescription() { return description; } public void setDescription(String description) { System.out.println("..Setting office description : " + description); this.description = description; } public Address getAddress() { return address; } public void setAddress(Address address) { System.out.println("..Setting office address : " + address); this.address = address; } } package ex15.pyrmont.digestertest; public class Address { private String streetName; private String streetNumber; public Address() { System.out.println("....Creating Address"); } public String getStreetName() { return streetName; } public void setStreetName(String streetName) { System.out.println("....Setting streetName : " + streetName); this.streetName = streetName; } public String getStreetNumber() { return streetNumber; } public void setStreetNumber(String streetNumber) { System.out.println("....Setting streetNumber : " + streetNumber); this.streetNumber = streetNumber; } public String toString() { return "...." + streetNumber + " " + streetName; } }