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

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

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

Example xml 使用Hibernate.initialize解决no session的延迟加载问题

<?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);
    }

}

你可能感兴趣的:(使用Jakarta Common Digester解析XML的简单例子)