Digester 简单实例

<?xml version="1.0" encoding="UTF-8"?>
<web name = "web1">
	<root name = "root_name1">
		<bar id="0" title="The Zero Child">aaa</bar>
		<bar id="123" title="The First Child">bbb</bar>
		<bar id="456" title="The Second Child">ccc</bar>
	</root>
	<root name = "root_name2">
		<bar id="789" title="Another Child">ddd</bar>
	</root>
</web>


1、根据xml文件信息,从底向上定义三个类,分别为 Bar.java,Root.java,Web.java

2、Bar.java 属性有:id,title,info
   Root.java 属性有:name,Vector<Bar>
   Web.java 属性有:name,Vector<Root>

3、定义一个测试类Test.java
   3.1 为Digester定义规则
   3.2 取得数据并对数据操作

public class Bar {

    private String id;

    private String title;

    private String info;

    public String getId() {
        return id;
    }

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

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public String getTitle() {
        return title;
    }

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

}


public class Root {

    private Vector<Bar> vector = new Vector<Bar>();

    private String name;

    public String getName() {
        return name;
    }

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

    public Vector<Bar> getVector() {
        return vector;
    }

    public void addVector(Bar o) {
        vector.add(o);
    }

}


public class Web {
    
    private String name;
    
    private Vector<Root> vector = new Vector<Root>();

    public Vector<Root> getVector() {
        return vector;
    }

    public void addVector(Root root) {
        vector.add(root);
    }

    public String getName() {
        return name;
    }

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

}


public class Test {

    public static void main(String[] args) {
        
        //创建digester
        Digester d = new Digester();
        //碰到web时的动作
        d.addObjectCreate("web", Web.class);
        //设置Web类里的属性
        d.addSetProperties("web", "name", "name");
        //碰到web/root时的动作
        d.addObjectCreate("web/root", Root.class);
        //设置Root类的属性
        d.addSetProperties("web/root", "name", "name");
        //碰到web/root/bar时的动作
        d.addObjectCreate("web/root/bar", Bar.class);
        //设置Bar类的属性
        d.addSetProperties("web/root/bar", "id", "id");
        d.addSetProperties("web/root/bar", "title", "title");
        //设置Bar类的info属性 对应xml的值为:<bar>info</bar>
        d.addBeanPropertySetter("web/root/bar","info");
        //碰到web/root/bar结束时 调用上一层即Root类的addVector方法
        d.addSetNext("web/root/bar", "addVector");
        //碰到web/root结束时 调用上一层即Web类的addVector方法
        d.addSetNext("web/root", "addVector");
        
        try {
            //通过上面定义好的Digester规则 解析xml文件
            Web web = (Web) d.parse(new File("d:/test/test.xml"));
            
            if (web != null){//对获取的数据进行操作
                String webName = web.getName();
                System.out.println("webName:=" + webName);
                Vector<Root> roots = web.getVector();
                for (Iterator<Root> i = roots.iterator(); i.hasNext();){
                    Root root = i.next();
                    String rootName = root.getName();
                    System.out.println("rootName:=" + rootName);
                    Vector<Bar> bars = root.getVector();
                    for (Iterator<Bar> j = bars.iterator(); j.hasNext();){
                        Bar bar = j.next();
                        String barId = bar.getId();
                        String barTitle = bar.getTitle();
                        String barInfo = bar.getInfo();
                        System.out.println("barId:=" + barId);
                        System.out.println("barTitle:=" + barTitle);
                        System.out.println("barInfo:=" + barInfo);
                    }
                }
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        catch (SAXException e) {
            e.printStackTrace();
        }

    }

}

你可能感兴趣的:(java,Web,xml,J#)