XML digester的详细讲解
/** * * @param args * * @author William * @throws SAXException * @throws IOException * @date Aug 8, 2013 */public static void main(String[] args) throws IOException, SAXException {Digester digester = new Digester();digester.setNamespaceAware(false);digester.setValidating(false);digester.addObjectCreate("school", com.test.School.class);digester.addSetProperties("school");digester.addObjectCreate("school/teacher", com.test.Teacher.class);digester.addSetProperties("school/teacher");digester.addSetNext("school/teacher", "addTeacher");digester.addObjectCreate("school/teacher/students/student", com.test.Student.class);digester.addSetProperties("school/teacher/students/student");digester.addSetNestedProperties("school/teacher/students/student");digester.addSetNext("school/teacher/students/student/", "addStudent");School s = (School) digester.parse(new File("D:\\Indigo\\SFA workspace\\Test\\src\\com\\test\\NewFile.xml"));//System.out.println(s);System.out.println(s.teachers.size());for(int i=0;i<s.teachers.size();i++){Teacher st=s.getTeachers().get(i);for(int j=0;j<st.students.size();j++){Student std=st.students.get(j);System.out.println(st.getName()+","+std.getAge()+","+std.getHobby()+","+std.getId());}//System.out.println(st.students.size());}// /*通过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);// // }// }}}
?
?
?
<?xml version="1.0" encoding="UTF-8"?><school name="school number1" location="SZ"><teacher id="001" name="zhangshan"><students><student name="stu1"><hobby>basketball</hobby><id>111</id><age>23</age></student><student name="stu2"><hobby>football</hobby><id>222</id><age>24</age></student><student name="ggg"><hobby>gggg</hobby><id>333</id><age>21</age></student></students></teacher><teacher id="002" name="lisi"><students><student name="_stu1"><hobby>tennis</hobby><id>dddddddddddddddddd</id><age>25</age></student><student name="_stu2"><hobby>badminton</hobby><id>555</id><age>26</age></student></students></teacher></school>
?
/** * * * @author William * @date Aug 8, 2013 * @version 1.0 */public class School {public List<Teacher> getTeachers() {return teachers;}public void setTeachers(List<Teacher> teachers) {this.teachers = teachers;}public String name;public String location;List<Teacher> teachers=new ArrayList<Teacher>();public String getName() {return name;}public void setName(String name) {this.name = name;}public String getLocation() {return location;}public void setLocation(String location) {this.location = location;}public void addTeacher(Teacher t){teachers.add(t);}//@Override//public String toString() {//String s="School [name=" + name + ", location=" + location + "]";//s+="\nhave "+teachers.size()+" teachers\n";//for(Teacher t:teachers){//s+=t.toString();//s+="\n\t";//for(Student ss:t.students){//s+=ss;//s+="\n\t";//}//s+="\n";//}//return s;//}}
?
/** * * * @author William * @date Aug 8, 2013 * @version 1.0 */public class Student {private String id;public String getId() {return id;}public void setId(String id) {this.id = id;}private String name;private String hobby;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getHobby() {return hobby;}public void setHobby(String hobby) {this.hobby = hobby;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Student [name=" + name + ", hobby=" + hobby + ", age=" + age+ "]";}}
?
?
?
?
?
?
?
?
?
?
/** * * * @author William * @date Aug 8, 2013 * @version 1.0 */public class Teacher {private int id;private String name;public List<Student> students=new ArrayList<Student>();public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public void addStudent(Student s){students.add(s);}@Overridepublic String toString() {return "Teacher [id=" + id + ", name=" + name + "]";}}
?
?
?
commons-digester-1.6.jar
commons-collections-3.1.jar
commons-logging-1.0.4.jar
commons-beanutils-1.7.0.jar?
?
?
?
?
http://blog.csdn.net/possibleonline/article/details/6956133
?
http://www.codesky.net/article/200502/54795.html