使用accepter遍历所有XML子元素

public class Myvisitor extends VisitorSupport {
    /**
     * 对于属性节点,打印属性的名字和值
     */
    public void visit(Attribute node) {
        System.out.println("attribute : " + node.getName() + " = "
               + node.getValue());
    }

    /**
     * 对于处理指令节点,打印处理指令目标和数据
     */
    public void visit(ProcessingInstruction node) {
        System.out.println("PI : " + node.getTarget() + " "
               + node.getData());
    }

    /**
     * 对于元素节点,判断是否只包含文本内容,如是,则打印标记的名字和 元素的内容。如果不是,则只打印标记的名字
     */
    public void visit(Element node) {
        if (node.isTextOnly())
           System.out.println("element : " + node.getName() + " = "
                  + node.getText());
        else
           System.out.println("--------" + node.getName() + "--------");
    }
 
    @Test
    public void test() throws DocumentException {
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read("src/book.xml");
        // dom4jParser.traversalDocumentByIterator();
        document.accept(new Myvisitor());
     }
    

posted on 2012-06-14 22:45 youngturk 阅读(229) 评论(0)  编辑  收藏 所属分类: 几种文档解析web.xml解析


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


网站导航:
 
<2012年6月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

导航

统计

公告

this year :
1 jQuery
2 freemarker
3 框架结构
4 口语英语

常用链接

留言簿(6)

随笔分类

随笔档案

文章分类

文章档案

相册

EJB学习

Flex学习

learn English

oracle

spring MVC web service

SQL

Struts

生活保健

解析文件

搜索

最新评论

阅读排行榜

评论排行榜