var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-20738293-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script')"/>
jutleo
欢迎走进有风的地方~~
posts - 63,  comments - 279,  trackbacks - 0
废话不说了,看了上篇Visitor访问者模式---------学习dom4j时遇到的顺便拿来交流 这个一看就懂了
package org.bulktree.xml;

import java.io.File;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.VisitorSupport;
import org.dom4j.io.SAXReader;

/**
 * dom4j访问者模式解析xml文档
 * 
@author bulktree Email: laoshulin@gmail.com
 * @date Aug 10, 2008
 
*/

public class ReadXmlVisitor {

    ReadXmlVisitor() 
{

        File file 
= new File("student.xml");
        SAXReader saxReader 
= new SAXReader();

        
try {
            Document doc 
= saxReader.read(file);
            doc.accept(
new MyVisitor());
        }
 catch (DocumentException e) {
            e.printStackTrace();
        }

    }

    
    
public static void main(String[] args) {
        
new ReadXmlVisitor();
    }


}


/*
 * org.dom4j 包里有Visitor接口,VisitorSupport是它的实现类,定义了多个重载的visit方法
 
*/

class MyVisitor extends VisitorSupport {
    
public void visit(Attribute attr) {
        String name 
= attr.getName();
        String value 
= attr.getValue();

        System.out.println(
"Attribute--> " + name + " : " + value);
    }


    
public void visit(Element element) {
        String name 
= element.getName();
        
if (element.isTextOnly()) {
            System.out
                    .println(
"Element--> " + name + " : " + element.getText());
        }
 else {
            System.out.println(
"Element-->" + name);
        }

    }

}

posted on 2008-08-10 12:52 凌晨风 阅读(3022) 评论(0)  编辑  收藏 所属分类: Java学习笔记

只有注册用户登录后才能发表评论。


网站导航:
 

<2008年8月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿(11)

我参与的团队

随笔分类

随笔档案

文章分类

文章档案

新闻分类

新闻档案

收藏夹

围脖

最新随笔

搜索

  •  

最新评论

阅读排行榜

评论排行榜