網站:JavaEye 作者:yuping322 發表時間: 2007-08-22 18:10 此文章來自于 http://www.JavaEye.com
聲明:本文系JavaEye網站原創文章,未經JavaEye網站或者作者本人書面許可,任何其他網站嚴禁擅自發表本文,否則必將追究法律責任!
原文鏈接: http://yuping.javaeye.com/blog/115336

 1.   讀取并解析XML文檔:
讀寫XML文檔主要依賴于org.dom4j.io包,其中提供DOMReaderSAXReader兩類不同方式,而調用方式是一樣的。這就是依靠接口的好處。








 

    // 從文件讀取XML,輸入文件名,返回XML文檔

    public Document read(String fileName) throws MalformedURLException, DocumentException {

       SAXReader reader = new SAXReader();

       Document document = reader.read(new File(fileName));

       return document;

    }

 


其中,reader的read方法是重載的,可以從InputStream, File, Url等多種不同的源來讀取。得到的Document對象就帶表了整個XML。

根據本人自己的經驗,讀取的字符編碼是按照XML文件頭定義的編碼來轉換。如果遇到亂碼問題,注意要把各處的編碼名稱保持一致即可。

 

2.  取得Root節點

讀取后的第二步,就是得到Root節點。熟悉XML的人都知道,一切XML分析都是從Root元素開始的。








 

  public Element getRootElement(Document doc){

       return doc.getRootElement();

    }

 


2.2訪問節點


List childrenList = elt.elements();


List childrenList = elt.elements("student");


Element eltChild = elt.element("student");


2.3.訪問屬性


List  attrList = elt.attributes();


Attribute attr = elt.attribute("sn");


String attrValue = elt.attributeValue("sn");


2.4.刪除元素和屬性


Element eltStu = root.element("student");


root.remove(eltstu);


--------------------------------


etl.remove(elt.attribute("sn"));


3.    遍歷XML樹


DOM4J提供至少3種遍歷節點的方法:

1) 枚舉(Iterator)








 

    // 枚舉所有子節點

    for ( Iterator i = root.elementIterator(); i.hasNext(); ) {

       Element element = (Element) i.next();

       // do something

    }

    // 枚舉名稱為foo的節點

    for ( Iterator i = root.elementIterator("foo"); i.hasNext();) {

       Element foo = (Element) i.next();

       // do something

    }

    // 枚舉屬性

    for ( Iterator i = root.attributeIterator(); i.hasNext(); ) {

       Attribute attribute = (Attribute) i.next();

       // do something

    }


2)遞歸


遞歸也可以采用Iterator作為枚舉手段,但文檔中提供了另外的做法








 

    public void treeWalk() {

       treeWalk(getRootElement());

    }

    public void treeWalk(Element element) {

       for (int i = 0, size = element.nodeCount(); i < size; i++)     {

           Node node = element.node(i);

           if (node instanceof Element) {

              treeWalk((Element) node);

           } else { // do something....

           }

       }

}

 


 

3) Visitor模式

最令人興奮的是DOM4J對Visitor的支持,這樣可以大大縮減代碼量,并且清楚易懂。了解設計模式的人都知道,Visitor是GOF設計模式之一。其主要原理就是兩種類互相保有對方的引用,并且一種作為Visitor去訪問許多Visitable。我們來看DOM4J中的Visitor模式(快速文檔中沒有提供)

只需要自定一個類實現Visitor接口即可。








 

        public class MyVisitor extends VisitorSupport {

           public void visit(Element element){

               System.out.println(element.getName());

           }

           public void visit(Attribute attr){

               System.out.println(attr.getName());

           }

        }

 

        調用:  root.accept(new MyVisitor())


    Visitor接口提供多種Visit()的重載,根據XML不同的對象,將采用不同的方式來訪問。上面是給出的Element和Attribute的簡單實現,一般比較常用的就是這兩個。VisitorSupport是DOM4J提供的默認適配器,Visitor接口的Default Adapter模式,這個模式給出了各種visit(*)的空實現,以便簡化代碼。

    注意,這個Visitor是自動遍歷所有子節點的。如果是root.accept(MyVisitor),將遍歷子節點。我第一次用的時候,認為是需要自己遍歷,便在遞歸中調用Visitor,結果可想而知。

 

4. XPath支持

    DOM4J對XPath有良好的支持,如訪問一個節點,可直接用XPath選擇。








 

   public void bar(Document document) {

        List list = document.selectNodes( "//foo/bar" );

        Node node = document.selectSingleNode("//foo/bar/author");

        String name = node.valueOf( "@name" );

     }

 


    例如,如果你想查找XHTML文檔中所有的超鏈接,下面的代碼可以實現:








 

    public void findLinks(Document document) throws DocumentException {

        List list = document.selectNodes(" //a/@href ");

        for (Iterator iter = list.iterator(); iter.hasNext(); ) {

            Attribute attribute = (Attribute) iter.next();

            String url = attribute.getValue();

        }

     }

 


5. 字符串與XML的轉換


有時候經常要用到字符串轉換為XML或反之,








 

    // XML轉字符串

 Document document = ...;

    String text = document.asXML();

// 字符串轉XML

    String text = "<person> <name>James</name> </person>";

    Document document = DocumentHelper.parseText(text);

 


6 XSLT轉換XML









 

   public Document styleDocument(

       Document document,

       String stylesheet

    ) throws Exception {

    // load the transformer using JAXP

    TransformerFactory factory = TransformerFactory.newInstance();

    Transformer transformer = factory.newTransformer(

       new StreamSource( stylesheet )

    );

    // now lets style the given document

    DocumentSource source = new DocumentSource( document );

    DocumentResult result = new DocumentResult();

    transformer.transform( source, result );

    // return the transformed document

    Document transformedDoc = result.getDocument();

    return transformedDoc;

}

 


 

7. 創建XML

 一般創建XML是寫文件前的工作,這就像StringBuffer一樣容易。








 

    public Document createDocument() {

       Document document = DocumentHelper.createDocument();

       Element root = document.addElement(root);

       Element author1 =

           root

              .addElement("author")

              .addAttribute(name, "James")

              .addAttribute("location", "UK")

              .addText("James Strachan");

       Element author2 =

           root

              .addElement("author")

              .addAttribute("name", Bob")

              .addAttribute("location", "US")

              .addText("Bob McWhirter");

       return document;

    }

 


 

8. 文件輸出

    一個簡單的輸出方法是將一個Document或任何的Node通過write方法輸出








 

    FileWriter out = new FileWriter( "foo.xml");

    document.write(out);

 


 如果你想改變輸出的格式,比如美化輸出或縮減格式,可以用XMLWriter








 

    public void write(Document document) throws IOException {

       // 指定文件

       XMLWriter writer = new XMLWriter(

           new FileWriter( "output.xml" )

       );

       writer.write( document );

       writer.close();

       // 美化格式

       OutputFormat format = OutputFormat.createPrettyPrint();

       writer = new XMLWriter( System.out, format );

       writer.write( document );

       // 縮減格式

       format = OutputFormat.createCompactFormat();

       writer = new XMLWriter( System.out, format );

       writer.write( document );

    }

 





《 xml文件解析-DOM4J 》 的評論也很精彩,歡迎您也添加評論。查看詳細 >>





JavaEye推薦
上海樂福狗信息技術有限公司:誠聘技術經理和開發工程師
免費下載IBM社區版軟件--它基于開放的標準,支持廣泛的開發類型,讓您的開發高效自主!
京滬穗蓉四地免費注冊,SOA技術高手匯聚交鋒.
上海:優秀公司德比:高薪誠聘 資深Java工程師
廣州:優易公司:誠聘Java工程師,開發經理
上海:尤恩斯國際集團:誠聘開發工程師
北京:優秀公司NHNChina招聘:WEB開發,系統管理,JAVA開發, DBA



文章來源: http://yuping.javaeye.com/blog/115336