digester解析xml文件

1、下载commons-digester1.6.jar

2、新建一java工程。

3、在模块目录下根据需要建立4个package——action,dto,input,config.

4、在input内存放input.xml文件。其内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<root>
	<models>
		<model id="0001" name="测试一">
			<book id="20091113" name="中国书"/>
			<book id="20091113" name="中国书"/>
			<magazine id="2009113" name="中国杂志" time="2009"/>
			<magazine id="2009113" name="中国杂志" time="2009"/>
		</model>
		<model id="0002" name="测试二">
			<book id="20091113" name="中国书"/>
			<book id="20091113" name="中国书"/>
			<magazine id="2009113" name="中国杂志" time="2009"/>
			<magazine id="2009113" name="中国杂志" time="2009"/>
		</model>
	</models>
</root>

  

5、在dto内新建5个java bean。(根据实际(xml文件)情况而定)。其中包含get,set方法。以及add(下一级节点名称)方法。

     5.1、Root.java

package com.test.digester.test.dto;

import java.util.Vector;

public class Root {
	private Vector<Models> models = new Vector<Models>();

	public Vector<Models> getModels() {
		return models;
	}

	public void setModels(Vector<Models> models) {
		this.models = models;
	}

	public void addModels(Models obj) {
		models.addElement(obj);
		setModels(this.models);
	}

}

 

     5.2、Models.java

package com.test.digester.test.dto;

import java.util.Vector;

public class Models {
	private Vector<Model> models = new Vector<Model>();

	public Vector<Model> getModels() {
		return models;
	}

	public void setModels(Vector<Model> models) {
		this.models = models;
	}

	public void addModel(Model obj) {
		models.addElement(obj);
		setModels(this.models);
	}
}

 

     5.3、Molde.java

package com.test.digester.test.dto;

import java.util.Vector;

public class Model {
	private String id;
	private String name;
	private Vector<Book> books = new Vector<Book>();
	private Vector<Magazine> magazines = new Vector<Magazine>();

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

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

	public Vector<Book> getBooks() {
		return books;
	}

	public void setBooks(Vector<Book> books) {
		this.books = books;
	}

	public Vector<Magazine> getMagazines() {
		return magazines;
	}

	public void setMagazines(Vector<Magazine> magazines) {
		this.magazines = magazines;
	}

	public void addBook(Book obj) {
		books.addElement(obj);
		setBooks(this.books);
	}

	public void addMagazine(Magazine obj) {
		magazines.addElement(obj);
		setMagazines(this.magazines);
	}
}

 

     5.4、Book.java

package com.test.digester.test.dto;

public class Book {
	private String id;
	private String name;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

}

 

     5.5、Magazine.java

package com.test.digester.test.dto;

public class Magazine {
	private String id;
	private String name;
	private String time;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getTime() {
		return time;
	}
	public void setTime(String time) {
		this.time = time;
	}
}

 

6、在config内创建config.xml文件,定义其解析规则:

<?xml version="1.0" encoding="UTF-8"?>
<digester-rules>
	<pattern value="root">
		<object-create-rule classname="com.inspur.digester.test.dto.Root" />
	</pattern>
	<pattern value="root/models">
		<object-create-rule classname="com.inspur.digester.test.dto.Models" />
		<set-next-rule methodname="addModels" />
	</pattern>
	<pattern value="root/models/model">
		<object-create-rule classname="com.inspur.digester.test.dto.Model" />
		<set-properties-rule >
			<alias attr-name="id" prop-name="id" />
			<alias attr-name="name" prop-name="name" />
		</set-properties-rule>
		<set-next-rule methodname="addModel" />
	</pattern>
	<pattern value="root/models/model/book">
		<object-create-rule classname="com.inspur.digester.test.dto.Book" />
		<set-properties-rule >
			<alias attr-name="id" prop-name="id" />
			<alias attr-name="name" prop-name="name" />
		</set-properties-rule>
		<set-next-rule methodname="addBook" />
	</pattern>
	<pattern value="root/models/model/magazine">
		<object-create-rule classname="com.inspur.digester.test.dto.Magazine" />
		<set-properties-rule >
			<alias attr-name="id" prop-name="id" />
			<alias attr-name="name" prop-name="name" />
			<alias attr-name="time" prop-name="time" />
		</set-properties-rule>
		<set-next-rule methodname="addMagazine"/>
	</pattern>
</digester-rules>

 

7、在action内新建一Test.java。内容如下:

package com.test.digester.test.action;

import java.io.File;

import org.apache.commons.digester.Digester;
import org.apache.commons.digester.xmlrules.DigesterLoader;

import com.test.digester.test.dto.Root;

public class Test {
	public static void main(String[] args) {
		try {
			String inputFile="src/com/test/digester/test/input/input.xml";
			String rulesFile="src/com/test/digester/test/config/config.xml";
			File input = new File(inputFile);
			File rules = new File(rulesFile);

			Digester digester = DigesterLoader.createDigester(rules.toURL());

			Root root = (Root) digester.parse(input);
			System.out.println(root.toString());

		} catch (Exception exc) {
			exc.printStackTrace();
		}
	}
}

 

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