XML解析之Digester

Digester package lets you configure an XML -> Java object mapping module, which triggers certain actions called rules whenever a particular pattern of nested XML elements is recognized. A rich set of predefined rules is available for your use, or you can also create your own.

映射XML对象
public class User {

	private int id;
	private String name;
	private int age;
	private String sex;
	
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	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 String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	
	public String toString(){
		return "user: "+name+" "+sex;
	}
}


模拟用户注册表
public class UserRegistry {
	
	java.util.List<User> registry = new ArrayList<User>();

	public void register(User obj) {
		registry.add(obj);
	}

	public List<User> getRegistry() {
		return registry;
	}
	
	public void printRegistry(){
		for(int i =0;i<registry.size();i++){
			User user = registry.get(i);
			System.out.println(user);
		}
	}
}

Digester提供code解析和rule解析
public class DigesterParse {

	static File file = new File("user.xml");
	static File rule = new File("rule.xml");

	public static void main(String[] args) {
		System.out.println("*******Digester parsing by Code******");
		parseByCode().printRegistry();
		System.out.println("*******Digester parsing by Rule******");
		parseByRule().printRegistry();
	}

	public static UserRegistry parseByCode() {
		UserRegistry regs = null;
		InputStream input = null;
		Digester digester = null;
		try {
			input = new FileInputStream(file);
			digester = new Digester();
			// 不进行XML与相应的DTD的合法性验证
			digester.setValidating(false);
			// 创建一个UserRegistry对象,将其放到栈顶
			digester.push(new UserRegistry());
			// 当遇到<user>时创建一个User对象
			digester.addObjectCreate("users/user", User.class);
			// 根据<user>元素的属性,对刚创建的User对象的属性进行设置
			digester.addSetProperties("users/user");
			// 当遇到<user>的子元素<name>时将子元素值设置为User对象的name属性
			digester.addBeanPropertySetter("users/user/name");
			digester.addBeanPropertySetter("users/user/age");
			digester.addBeanPropertySetter("users/user/sex");
			// 当再次遇到<users>的子元素<user>时调用栈顶对象的register方法
			digester.addSetNext("users/user", "register");
			// 分析结束后,返回根元素
			regs = (UserRegistry) digester.parse(input);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}
		return regs;
	}

	public static UserRegistry parseByRule() {
		UserRegistry regs = null;
		Digester digester = null;
		try {
			digester = DigesterLoader.createDigester(rule.toURL());
			regs = (UserRegistry) digester.parse(file);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}
		return regs;
	}
}

Rule规则
<?xml version='1.0'?>
<!DOCTYPE digester-rules 
  PUBLIC "-//Jakarta Apache //DTD digester-rules XML V1.0//EN" 
    "file:///../digester/dtds/digester-rules.dtd">

<digester-rules>
    <pattern value="users">  <!-- 遇上users节点时 -->
        <object-create-rule classname="UserRegistry" />  <!-- 创建对象 -->
        <set-properties-rule /> <!-- 设置属性 -->
        <pattern value="user"> <!-- 遇上user节点时 -->
            <object-create-rule classname="User" /> <!-- 创建对象 -->
            <set-next-rule methodname="register" paramtype="User"/> 
            <set-properties-rule /> <!-- 设置属性 -->
            <bean-property-setter-rule pattern="id" /> <!-- 调用对象的setter方法 -->
            <bean-property-setter-rule pattern="name" />
            <bean-property-setter-rule pattern="age" />
            <bean-property-setter-rule pattern="sex" />
        </pattern>
    </pattern>
</digester-rules>



你可能感兴趣的:(apache,xml)