使用Digest xml Rule 解析xml

先说大致的原理:

Digester解析XML会按照预先定义的xml规则解释。
这个规则中几个重要的节点如下:
<object-create-rule>这个标签是创建一个java实列
<set-properties-rule/>会根据解析文件的 属性调用生成类的setXXX方法,列如要解析的XML中有name,id,nextid,那么生成的类里如果有这些属性将会被调用他们的set方法。
<bean-property-setter-rule pattern="name" />会根据pattern来获取xml中标签名字,取的值,然后调用相应的bean的set方法。
<set-next-rule methodname="addStudent" />栈顶元素的下面一个元素(对象)的方法。
digester解析XML和他们大部分解析技术都一样,都用堆栈。当digester遇到一个<他会把这个元素压入堆栈,遇到/>就会把元素弹出堆栈。



1. 首先定义好对象,下面定义的对象只是测试用,无任何意义。
public class Student {

    String sex;
    String name;
    String age;
    Parent parent;

    public String getName() {
        return name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public Parent getParent() {
        return parent;
    }

    public void setParent(Parent parent) {
        this.parent = parent;
    }

}



public class Parent {

    String name;
    String age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

}




import java.util.ArrayList;
import java.util.List;

public class Students {

    List<Student> students;

    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }

    public void addStudent(Student student) {
        students.add(student);
    }

    public Students() {
        super();
        students = new ArrayList<Student>();
    }

    public void sayHello() {
        for (Student st : students) {
            System.out.println("hello !~ " + st.getName() + " sex: " + st.getSex());
            System.out.println("hello !~  parent" + st.getParent().getName());
        }
    }
}




import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;

import org.apache.commons.digester.Digester;
import org.apache.commons.digester.xmlrules.DigesterLoader;
import org.xml.sax.SAXException;

public class Parser {

    public static void main(String[] args) throws FileNotFoundException, IOException, SAXException, URISyntaxException {
        URL resource = Parser.class.getResource("studentRule.xml");
        URL xmlFile = Parser.class.getResource("students.xml");
        Digester digester = DigesterLoader.createDigester(resource);
        Students students = null;
        File file = new File(xmlFile.toURI().getPath());
        students = (Students) digester.parse(new FileInputStream(file));
        students.sayHello();
    }
}



2. 然后定义xml解析规则

<?xml version="1.0" encoding="UTF-8"?>
<digester-rules>
	<pattern value="students">
		<object-create-rule classname="digester.Students" />
		<pattern value="student">
			<!-- 创建对象 -->
			<object-create-rule classname="digester.Student" />
			<!-- 自动设置属性 -->
			<set-properties-rule />

			<!-- bean对象调用set方法的规则s -->
			<bean-property-setter-rule pattern="sex" />
			<bean-property-setter-rule pattern="age" />
			<bean-property-setter-rule pattern="name" />
			<!-- 调用 digester.Students.addStudent -->
			<set-next-rule methodname="addStudent" />
			<pattern value="parent">
				<object-create-rule classname="digester.Parent" />
				<set-properties-rule />
				<bean-property-setter-rule pattern="age" />
				<bean-property-setter-rule pattern="name" />
				<set-next-rule methodname="setParent" />
			</pattern>
		</pattern>
	</pattern>
</digester-rules>



3. xml文件
<?xml version="1.0" encoding="UTF-8"?>
<students>
	<student sex="F">
		<name>
			test1name
        </name>
		<age>
			test1age
        </age>
		<parent>
			<name>
				test1pname
           </name>
			<age>
				test1page
           </age>
		</parent>
	</student>
	<student sex="M">
		<name>
			test2name
        </name>
		<age>
			test2age
        </age>
		<parent>
			<name>
				test2pname
           </name>
			<age>
				test2page
           </age>
		</parent>
	</student>
	<student sex="M">
		<name>
			test3name
        </name>
		<age>
			test3age
        </age>
		<parent>
			<name>
				test3pname
           </name>
			<age>
				test3page
           </age>
		</parent>
	</student>
</students>


参考:http://blog.csdn.net/maomao1221/archive/2006/09/04/1174903.aspx

你可能感兴趣的:(java,apache,.net,xml,bean)