使用Jakarta Common Digester解析XML的简单例子

Jakarta Commons Digester官方网址
http://commons.apache.org/digester/
其中注意依赖包
BeanUtils和Logging


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

Example xml

<?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.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;
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方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值