dom4j操作xml基础--Visitor访问模式解析XML

http://www.blogjava.net/bulktree/archive/2008/08/10/221122.html

 

[IT科技]dom4j操作xml基础--Visitor访问模式解析XML

旧一篇: Thinking XML: Firefox 3.0 和 XML  新一篇: JSON辅助类,可以把一些对象和集合转化为标准的JSON格式 

http://www.blogjava.net/bulktree/archive/2008/08/10/221122.html

dom4j遍历xml文档树有种很特别的方式就是访问者(Visitor)模式,初次接触Visitor模式,写出个人理解大家交流!
Visitor访问者模式定义:作用于某个对象树中各个对象的操作. 它可以使你在不改变这些对象树本身的情况下,定义作用于这些对象树各个节点的新操作。
先看以下代码:Person为简单的vo类
  1. package org.bulktree.visitor;   
  2.   
  3. import java.util.ArrayList;   
  4. import java.util.Collection;   
  5. import java.util.Iterator;   
  6. import java.util.List;   
  7.   
  8. /**  
  9.  *   
  10.  * @author bulktree Email: <A href="mailto:laoshulin@gmail.com">laoshulin@gmail.com</A>  
  11.  * @date Aug 10, 2008  
  12.  */  
  13. public class ReadCollection {   
  14.   
  15.     private Collection c = null;   
  16.   
  17.     ReadCollection() {   
  18.   
  19.         /*  
  20.          * 准备数据-String对象-Person对象-Integer对象-List对象  
  21.          */  
  22.         String str = "bulktree.laoshulin";   
  23.         Person person = new Person("bulktree""22""M");   
  24.         Integer a = new Integer(99);   
  25.         /*  
  26.          * 使用范型  
  27.          */  
  28.         List<String> list = new ArrayList<String>();   
  29.         list.add("BULKTREE");   
  30.         list.add("LAOSHULIN");   
  31.         list.add("OAKERTREE");   
  32.   
  33.         c = new ArrayList();   
  34.         c.add(str);   
  35.         c.add(person);   
  36.         c.add(a);   
  37.         c.add(list);   
  38.   
  39.     }   
  40.   
  41.     /**  
  42.      * 遍历Collection中的每一个对象并打印  
  43.      */  
  44.     public void testCollection() {   
  45.         Iterator iter = getCollection().iterator();   
  46.   
  47.         while (iter.hasNext()) {   
  48.             Object o = iter.next();   
  49.   
  50.             if (o instanceof String) {   
  51.                 System.out.println("String-->  " + o.toString());   
  52.             } else if (o instanceof Person) {   
  53.                 readPerson((Person) o);   
  54.             } else if (o instanceof Integer) {   
  55.                 Integer inta = (Integer) o;   
  56.                 System.out.println(inta.intValue());   
  57.             } else if (o instanceof List) {   
  58.                 readList((List) o);   
  59.             }   
  60.         }   
  61.   
  62.     }   
  63.   
  64.     public Collection getCollection() {   
  65.         return c;   
  66.     }   
  67.   
  68.     private void readPerson(Person person) {   
  69.         System.out.println("person-name-> " + person.getName());   
  70.         System.out.println("person-age-> " + person.getAge());   
  71.         System.out.println("person-sex-> " + person.getSex());   
  72.     }   
  73.   
  74.     private void readList(List<String> list) {   
  75.         /*  
  76.          * 增强的for循环  
  77.          */  
  78.         for (String s : list) {   
  79.             System.out.println(s);   
  80.         }   
  81.     }   
  82.   
  83.     public static void main(String[] args) {   
  84.         new ReadCollection().testCollection();   
  85.     }   
  86. }  
package org.bulktree.visitor;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * 
 * @author bulktree Email: laoshulin@gmail.com
 * @date Aug 10, 2008
 */
public class ReadCollection {

    private Collection c = null;

    ReadCollection() {

        /*
         * 准备数据-String对象-Person对象-Integer对象-List对象
         */
        String str = "bulktree.laoshulin";
        Person person = new Person("bulktree", "22", "M");
        Integer a = new Integer(99);
        /*
         * 使用范型
         */
        List<String> list = new ArrayList<String>();
        list.add("BULKTREE");
        list.add("LAOSHULIN");
        list.add("OAKERTREE");

        c = new ArrayList();
        c.add(str);
        c.add(person);
        c.add(a);
        c.add(list);

    }

    /**
     * 遍历Collection中的每一个对象并打印
     */
    public void testCollection() {
        Iterator iter = getCollection().iterator();

        while (iter.hasNext()) {
            Object o = iter.next();

            if (o instanceof String) {
                System.out.println("String-->  " + o.toString());
            } else if (o instanceof Person) {
                readPerson((Person) o);
            } else if (o instanceof Integer) {
                Integer inta = (Integer) o;
                System.out.println(inta.intValue());
            } else if (o instanceof List) {
                readList((List) o);
            }
        }

    }

    public Collection getCollection() {
        return c;
    }

    private void readPerson(Person person) {
        System.out.println("person-name-> " + person.getName());
        System.out.println("person-age-> " + person.getAge());
        System.out.println("person-sex-> " + person.getSex());
    }

    private void readList(List<String> list) {
        /*
         * 增强的for循环
         */
        for (String s : list) {
            System.out.println(s);
        }
    }

    public static void main(String[] args) {
        new ReadCollection().testCollection();
    }
}


我们使用了 instanceof来判断 Object对象 o 的类型,这样做的缺点是代码中If/else if 很繁琐,而JDK中的范型又限制了只能使用相同的类型,这时Vistor访问模式派上用场了。

当我们要访问Collection的每一个Element(被访问者)时,定义一个accept操作使其具有可被访问性,我们定义一个Visiable接口,使Collection的每一个Element继承这个接口,实现自身的访问操作

package org.bulktree.visitor;

  1. /**  
  2.  * 可访问性--接收一个访问者  
  3.  * @author bulktree Email: <A href="mailto:laoshulin@gmail.com">laoshulin@gmail.com</A>  
  4.  * @date Aug 10, 2008  
  5.  */  
  6. public interface Visitable {   
  7.     public void accept(Visitor visitor);   
  8. }  
/**
 * 可访问性--接收一个访问者
 * @author bulktree Email: laoshulin@gmail.com
 * @date Aug 10, 2008
 */
public interface Visitable {
    public void accept(Visitor visitor);
}


下来是四个被访问的类型String,Integer,Person,Collection的实现类
  1. package org.bulktree.visitor;   
  2.   
  3. /**  
  4.  * 被访问者--String对象  
  5.  * @author bulktree Email: <A href="mailto:laoshulin@gmail.com">laoshulin@gmail.com</A>  
  6.  * @date Aug 10, 2008  
  7.  */  
  8. public class StringElement implements Visitable {   
  9.   
  10.     private String str;   
  11.   
  12.     public StringElement(String str) {   
  13.         this.str = str;   
  14.     }   
  15.   
  16.     public String getStr() {   
  17.         return str;   
  18.     }   
  19.   
  20.     public void accept(Visitor visitor) {   
  21.         visitor.visitString(this);   
  22.     }   
  23. }  
package org.bulktree.visitor;

/**
 * 被访问者--String对象
 * @author bulktree Email: laoshulin@gmail.com
 * @date Aug 10, 2008
 */
public class StringElement implements Visitable {

    private String str;

    public StringElement(String str) {
        this.str = str;
    }

    public String getStr() {
        return str;
    }

    public void accept(Visitor visitor) {
        visitor.visitString(this);
    }
}


  1. package org.bulktree.visitor;   
  2.   
  3. /**  
  4.  * 被访问者--Integer对象  
  5.  * @author bulktree Email: <A href="mailto:laoshulin@gmail.com">laoshulin@gmail.com</A>  
  6.  * @date Aug 10, 2008  
  7.  */  
  8. public class IntegerElement implements Visitable {   
  9.   
  10.     private Integer i;   
  11.        
  12.     public IntegerElement(Integer i) {   
  13.         this.i = i;   
  14.     }   
  15.        
  16.     public Integer getI() {   
  17.         return i;   
  18.     }   
  19.        
  20.     public void accept(Visitor visitor) {   
  21.         visitor.visitInteger(this);   
  22.   
  23.     }   
  24. }  
package org.bulktree.visitor;

/**
 * 被访问者--Integer对象
 * @author bulktree Email: laoshulin@gmail.com
 * @date Aug 10, 2008
 */
public class IntegerElement implements Visitable {

    private Integer i;
    
    public IntegerElement(Integer i) {
        this.i = i;
    }
    
    public Integer getI() {
        return i;
    }
    
    public void accept(Visitor visitor) {
        visitor.visitInteger(this);

    }
}


  1. package org.bulktree.visitor;   
  2.   
  3. import java.util.Collection;   
  4.   
  5. /**  
  6.  * 被访问者--Person对象  
  7.  * @author bulktree Email: <A href="mailto:laoshulin@gmail.com">laoshulin@gmail.com</A>  
  8.  * @date Aug 10, 2008  
  9.  */  
  10. public class PersonElement implements Visitable{   
  11.     private Person p;   
  12.        
  13.     public PersonElement(Person p) {   
  14.         this.p = p;   
  15.     }   
  16.        
  17.     public Person getP() {   
  18.         return p;   
  19.     }   
  20.   
  21.     public void accept(Visitor visitor) {   
  22.         visitor.visitPerson(this);   
  23.     }   
  24. }  
package org.bulktree.visitor;

import java.util.Collection;

/**
 * 被访问者--Person对象
 * @author bulktree Email: laoshulin@gmail.com
 * @date Aug 10, 2008
 */
public class PersonElement implements Visitable{
    private Person p;
    
    public PersonElement(Person p) {
        this.p = p;
    }
    
    public Person getP() {
        return p;
    }

    public void accept(Visitor visitor) {
        visitor.visitPerson(this);
    }
}


  1. package org.bulktree.visitor;   
  2.   
  3. import java.util.Collection;   
  4. import java.util.List;   
  5.   
  6. /**  
  7.  * 被访问者--Collection对象  
  8.  * @author bulktree Email: <A href="mailto:laoshulin@gmail.com">laoshulin@gmail.com</A>  
  9.  * @date Aug 10, 2008  
  10.  */  
  11. public class CollectionElement implements Visitable {   
  12.   
  13.     private Collection collection;   
  14.   
  15.     public CollectionElement(Collection collection) {   
  16.         this.collection = collection;   
  17.     }   
  18.   
  19.     public Collection getCollection() {   
  20.         return collection;   
  21.     }   
  22.   
  23.     public void accept(Visitor visitor) {   
  24.         visitor.visitCollection(collection);   
  25.     }   
  26. }  
package org.bulktree.visitor;

import java.util.Collection;
import java.util.List;

/**
 * 被访问者--Collection对象
 * @author bulktree Email: laoshulin@gmail.com
 * @date Aug 10, 2008
 */
public class CollectionElement implements Visitable {

    private Collection collection;

    public CollectionElement(Collection collection) {
        this.collection = collection;
    }

    public Collection getCollection() {
        return collection;
    }

    public void accept(Visitor visitor) {
        visitor.visitCollection(collection);
    }
}


下来定义一个访问者Visitor接口,它可以访问Integer,String,Person(VO对象),Collection类型
  1. package org.bulktree.visitor;   
  2.   
  3. import java.util.Collection;   
  4.   
  5. /**  
  6.  * 访问者接口  
  7.  * @author bulktree Email: <A href="mailto:laoshulin@gmail.com">laoshulin@gmail.com</A>  
  8.  * @date Aug 10, 2008  
  9.  */  
  10. public interface Visitor {   
  11.     public void visitString(StringElement str);   
  12.     public void visitInteger(IntegerElement i);   
  13.     public void visitCollection(Collection collection);   
  14.     public void visitPerson(PersonElement perE);   
  15. }  
package org.bulktree.visitor;

import java.util.Collection;

/**
 * 访问者接口
 * @author bulktree Email: laoshulin@gmail.com
 * @date Aug 10, 2008
 */
public interface Visitor {
    public void visitString(StringElement str);
    public void visitInteger(IntegerElement i);
    public void visitCollection(Collection collection);
    public void visitPerson(PersonElement perE);
}


关键的Visitor实现类
  1. package org.bulktree.visitor;   
  2.   
  3. import java.util.Collection;   
  4. import java.util.Iterator;   
  5.   
  6. /**  
  7.  * 访问者实现类  
  8.  * @author bulktree Email: <A href="mailto:laoshulin@gmail.com">laoshulin@gmail.com</A>  
  9.  * @date Aug 10, 2008  
  10.  */  
  11. public class VisitorImpl implements Visitor {   
  12.   
  13.     /*  
  14.      *访问字符串,仅对字符串输出   
  15.      */  
  16.     public void visitString(StringElement str) {   
  17.         System.out.println("*******************字符串输出*************************");   
  18.         System.out.println(str.getStr());   
  19.     }   
  20.   
  21.     /**  
  22.      * 访问Integer类型  
  23.      */  
  24.     public void visitInteger(IntegerElement i) {   
  25.         System.out.println("*******************整型输出*************************");   
  26.         System.out.println(i.getI());   
  27.     }   
  28.   
  29.     /**  
  30.      * 访问Collection对象,遍历每一个元素  
  31.      * 使用了一个if语句判断属于Visitable哪一个被访问对象,然后调用相应的accept方法  
  32.      * 实现递归调用  
  33.      */  
  34.     public void visitCollection(Collection collection) {   
  35.         Iterator iter = collection.iterator();   
  36.         while (iter.hasNext()) {   
  37.             Object o = iter.next();   
  38.             if (o instanceof Visitable) {   
  39.                 ((Visitable) o).accept(this);   
  40.             }   
  41.         }   
  42.     }   
  43.   
  44.     /**  
  45.      * 访问单个Person对象  
  46.      */  
  47.     public void visitPerson(PersonElement perE) {   
  48.         System.out.println("*******************Person对象输出*************************");   
  49.         Person person = perE.getP();   
  50.         System.out.println("person-name-> " + person.getName());   
  51.         System.out.println("person-age-> " + person.getAge());   
  52.         System.out.println("person-sex-> " + person.getSex());   
  53.     }   
  54.   
  55. }  
package org.bulktree.visitor;

import java.util.Collection;
import java.util.Iterator;

/**
 * 访问者实现类
 * @author bulktree Email: laoshulin@gmail.com
 * @date Aug 10, 2008
 */
public class VisitorImpl implements Visitor {

    /*
     *访问字符串,仅对字符串输出 
     */
    public void visitString(StringElement str) {
        System.out.println("*******************字符串输出*************************");
        System.out.println(str.getStr());
    }

    /**
     * 访问Integer类型
     */
    public void visitInteger(IntegerElement i) {
        System.out.println("*******************整型输出*************************");
        System.out.println(i.getI());
    }

    /**
     * 访问Collection对象,遍历每一个元素
     * 使用了一个if语句判断属于Visitable哪一个被访问对象,然后调用相应的accept方法
     * 实现递归调用
     */
    public void visitCollection(Collection collection) {
        Iterator iter = collection.iterator();
        while (iter.hasNext()) {
            Object o = iter.next();
            if (o instanceof Visitable) {
                ((Visitable) o).accept(this);
            }
        }
    }

    /**
     * 访问单个Person对象
     */
    public void visitPerson(PersonElement perE) {
        System.out.println("*******************Person对象输出*************************");
        Person person = perE.getP();
        System.out.println("person-name-> " + person.getName());
        System.out.println("person-age-> " + person.getAge());
        System.out.println("person-sex-> " + person.getSex());
    }

}


客户端测试:
  1. package org.bulktree.visitor;   
  2.   
  3. import java.util.ArrayList;   
  4. import java.util.Collection;   
  5.   
  6. /**  
  7.  * Visitor模式客户端  
  8.  * @author bulktree Email: <A href="mailto:laoshulin@gmail.com">laoshulin@gmail.com</A>  
  9.  * @date Aug 10, 2008  
  10.  */  
  11. public class VisitorMain {   
  12.   
  13.     public static void main(String[] args) {   
  14.         Visitor visitor = new VisitorImpl();   
  15.   
  16.            
  17.         /*  
  18.          * 访问字符串  
  19.          */  
  20.         System.out.println("======================访问字符串=========================");   
  21.         StringElement stringE = new StringElement(   
  22.                 "bulktree.laoshulin.oakertree");   
  23.         visitor.visitString(stringE);   
  24.   
  25.         /*  
  26.          * 访问集合  
  27.          */  
  28.         System.out.println("=======================访问集合========================");   
  29.         Collection list = new ArrayList();   
  30.   
  31.         StringElement str1 = new StringElement("aaa");   
  32.         StringElement str2 = new StringElement("bbb");   
  33.         list.add(str1);   
  34.         list.add(str2);   
  35.            
  36.         PersonElement perE1 = new PersonElement(new Person("LAOSHULIN""22""M"));   
  37.         PersonElement perE2 = new PersonElement(new Person("BULKTREE""21""W"));   
  38.         list.add(perE1);   
  39.         list.add(perE2);   
  40.            
  41.         IntegerElement intE1 = new IntegerElement(new Integer(99));   
  42.         IntegerElement intE2 = new IntegerElement(new Integer(100));   
  43.         list.add(intE1);   
  44.         list.add(intE2);   
  45.            
  46.         visitor.visitCollection(list);   
  47.   
  48.         /*  
  49.          * 访问Person  
  50.          */  
  51.         System.out.println("======================访问Person=========================");   
  52.         Person p = new Person("BULKTREE""22""M");   
  53.         PersonElement perE = new PersonElement(p);   
  54.         visitor.visitPerson(perE);   
  55.            
  56.         /*  
  57.          * 访问Integer  
  58.          */  
  59.         System.out.println("=====================访问Integer==========================");   
  60.         IntegerElement intE = new IntegerElement(new Integer(77));   
  61.         visitor.visitInteger(intE);   
  62.     }   
  63. }  
package org.bulktree.visitor;

import java.util.ArrayList;
import java.util.Collection;

/**
 * Visitor模式客户端
 * @author bulktree Email: laoshulin@gmail.com
 * @date Aug 10, 2008
 */
public class VisitorMain {

    public static void main(String[] args) {
        Visitor visitor = new VisitorImpl();

        
        /*
         * 访问字符串
         */
        System.out.println("======================访问字符串=========================");
        StringElement stringE = new StringElement(
                "bulktree.laoshulin.oakertree");
        visitor.visitString(stringE);

        /*
         * 访问集合
         */
        System.out.println("=======================访问集合========================");
        Collection list = new ArrayList();

        StringElement str1 = new StringElement("aaa");
        StringElement str2 = new StringElement("bbb");
        list.add(str1);
        list.add(str2);
        
        PersonElement perE1 = new PersonElement(new Person("LAOSHULIN", "22", "M"));
        PersonElement perE2 = new PersonElement(new Person("BULKTREE", "21", "W"));
        list.add(perE1);
        list.add(perE2);
        
        IntegerElement intE1 = new IntegerElement(new Integer(99));
        IntegerElement intE2 = new IntegerElement(new Integer(100));
        list.add(intE1);
        list.add(intE2);
        
        visitor.visitCollection(list);

        /*
         * 访问Person
         */
        System.out.println("======================访问Person=========================");
        Person p = new Person("BULKTREE", "22", "M");
        PersonElement perE = new PersonElement(p);
        visitor.visitPerson(perE);
        
        /*
         * 访问Integer
         */
        System.out.println("=====================访问Integer==========================");
        IntegerElement intE = new IntegerElement(new Integer(77));
        visitor.visitInteger(intE);
    }
}

使用访问者模式的前提是对象群结构中(Collection) 中的对象类型很少改变,在两个接口Visitor(访问)和Visitable(可访问)中,确保Visitable很少变化,也就是说,确保不能有新的元素类型加进来,可以变化的是访问者行为或操作,也就是Visitor的不同子类可以有多种,这样使用访问者模式最方便,当系统中存在着固定的数据结构,且有着不同的行为,访问者模式也许是个不错的选择

  1. package org.bulktree.xml;   
  2.   
  3. import java.io.File;   
  4.   
  5. import org.dom4j.Attribute;   
  6. import org.dom4j.Document;   
  7. import org.dom4j.DocumentException;   
  8. import org.dom4j.Element;   
  9. import org.dom4j.VisitorSupport;   
  10. import org.dom4j.io.SAXReader;   
  11.   
  12. /**  
  13.  * dom4j访问者模式解析xml文档  
  14.  * @author bulktree Email: <A href="mailto:laoshulin@gmail.com">laoshulin@gmail.com</A>  
  15.  * @date Aug 10, 2008  
  16.  */  
  17. public class ReadXmlVisitor {   
  18.   
  19.     ReadXmlVisitor() {   
  20.   
  21.         File file = new File("student.xml");   
  22.         SAXReader saxReader = new SAXReader();   
  23.   
  24.         try {   
  25.             Document doc = saxReader.read(file);   
  26.             doc.accept(new MyVisitor());   
  27.         } catch (DocumentException e) {   
  28.             e.printStackTrace();   
  29.         }   
  30.     }   
  31.        
  32.     public static void main(String[] args) {   
  33.         new ReadXmlVisitor();   
  34.     }   
  35.   
  36. }   
  37.   
  38. /*  
  39.  * org.dom4j 包里有Visitor接口,VisitorSupport是它的实现类,定义了多个重载的visit方法  
  40.  */  
  41. class MyVisitor extends VisitorSupport {   
  42.     public void visit(Attribute attr) {   
  43.         String name = attr.getName();   
  44.         String value = attr.getValue();   
  45.   
  46.         System.out.println("Attribute--> " + name + " : " + value);   
  47.     }   
  48.   
  49.     public void visit(Element element) {   
  50.         String name = element.getName();   
  51.         if (element.isTextOnly()) {   
  52.             System.out   
  53.                     .println("Element--> " + name + " : " + element.getText());   
  54.         } else {   
  55.             System.out.println("Element-->" + name);   
  56.         }   
  57.     }   
  58. }  
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值