JAVA使用Jakarta Common Digester解析XML示例指南

标签: Java XML
发布时间: 2011/5/11 9:33:05

使用Digester可以帮我们迅速的解析XML并且封装成BEAN对象。

<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE digester-rules PUBLIC "-//Jakarta Apache //DTD digester-rules XML V1.0//EN"
   "digester-rules.dtd">
<menu>
	<root title="网址">
		<node title="QQ" url="http://www.qq.com" ></node>
		<node title="163" url="http://www.163.com" ></node>
	</root>
</menu>

Beans

import java'>java.util.Vector;

/**
 * @author Kenny
 * 
 */
public class Menu {

	private Vector<Root> roots;

	public Menu() {
		roots = new Vector<Root>();
	}
	
	public void addRoot(Root root){
		roots.addElement(root);
	}

	public Vector<Root> getRoots() {
		return roots;
	}

	@Override
	public String toString() {
		return "Menu [roots=" + roots + "]";
	}

}
import java.util.Vector;

/**
 * @author Kenny
 * 
 */
public class Root {

	private String title;
	private Vector<Node> nodes;
	
	public Root() {
		nodes = new Vector<Node>(10);
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Vector<Node> getNode() {
		return nodes;
	}

	public void addNode(Node node) {
		nodes.addElement(node);
	}

	@Override
	public String toString() {
		return "Root [nodes=" + nodes + ", title=" + title + "]";
	}

}
/**
 * @author Kenny
 * 
 */
public class Node {

	private String title;
	private String url;

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	@Override
	public String toString() {
		return "Node [title=" + title + ", url=" + url + "]";
	}

}

Test Unit

import java.io.IOException'>Exception;
import java.net.URL;

import org.apache.commons.digester.Digester;
import org.junit.Assert;
import org.junit.Test;
import org.xml.sax.SAXException;

/**
 * @author Kenny
 * 
 */
public class TestDigest {

	@Test
	public void testParse() {
		URL xmlURL = this.getClass().getClassLoader().getResource("menu.xml");
		Digester digester = new Digester();
		digester.setValidating(false);
		digester.addObjectCreate("menu", Menu.class);
		digester.addObjectCreate("menu/root", Root.class);
		digester.addSetProperties("menu/root", "title","title");
		digester.addSetNext("menu/root", "addRoot");

		digester.addObjectCreate("menu/root/node", Node.class);
		digester.addSetProperties("menu/root/node", "title","title");
		digester.addSetProperties("menu/root/node", "url","url");
		digester.addSetNext("menu/root/node", "addNode");

		Menu menu = null;
		
		try {
			System.out.println(xmlURL.getPath());
			menu = (Menu) digester.parse(xmlURL);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}
		
		Assert.assertNotNull(menu);
		System.out.println(menu);
	}

}
其中 digester.addSetProperties 用来获取节点中的属性值。即<root title="网址">。
若是<root><title>网址</title></root>的形式,可以用digester.addBeanPropertySetter方法。

官方微信
官方QQ群
31647020