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 凌晨風 閱讀(3051) 評論(0)  編輯  收藏 所屬分類: Java學習筆記

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

          常用鏈接

          留言簿(11)

          我參與的團隊

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          新聞分類

          新聞檔案

          收藏夾

          圍脖

          最新隨筆

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 扬中市| 海阳市| 筠连县| 吴江市| 甘肃省| 沂水县| 永兴县| 湘乡市| 咸宁市| 和平县| 灌云县| 黄浦区| 准格尔旗| 元江| 云林县| 贵州省| 宜宾县| 论坛| 封开县| 手游| 廉江市| 卓资县| 博爱县| 武安市| 海阳市| 宁明县| 禄劝| 喀喇沁旗| 罗源县| 垦利县| 仁布县| 宜川县| 桃园县| 滦平县| 共和县| 伊吾县| 田阳县| 平阴县| 开原市| 灵寿县| 新龙县|