国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
dom4j的一般應(yīng)用 - bicashy - JavaEye技術(shù)網(wǎng)站
dom4j的官方地址:http://www.dom4j.org

   dom4j是一個(gè)成功的開(kāi)源項(xiàng)目,從java的對(duì)象概念的角度出發(fā),按照通常的使用規(guī)范來(lái)處理xml文檔,可以說(shuō),在java社區(qū)里,dom4j使用的最為廣泛了,以下是如,眾所周知的o/r mapping工具h(yuǎn)ibernate就是使用dom4j來(lái)解釋xml文檔的(ejb3.0就是基于hibernate實(shí)現(xiàn)的),由此可見(jiàn),dom4j的使用廣泛之處。dom4j的一個(gè)最大的實(shí)用之處是支持XPath表達(dá)式的查詢(xún),這樣,我們可以在dom4j的Document結(jié)構(gòu)中使用這種簡(jiǎn)單的表達(dá)式就可以快速找到我們需要的某個(gè)元素了。
Java代碼
  1. package com.gdapp.oa.app;   
  2.   
  3. import java.io.File;   
  4. import java.io.FileOutputStream;   
  5. import java.io.FileWriter;   
  6. import java.io.IOException;   
  7. import java.util.Iterator;   
  8.   
  9. import org.dom4j.Attribute;   
  10. import org.dom4j.Document;   
  11. import org.dom4j.DocumentException;   
  12. import org.dom4j.DocumentHelper;   
  13. import org.dom4j.Element;   
  14. import org.dom4j.Node;   
  15. import org.dom4j.io.OutputFormat;   
  16. import org.dom4j.io.SAXReader;   
  17. import org.dom4j.io.XMLWriter;   
  18.   
  19. /**  
  20.  * 對(duì)dom4j的操作,添加,修改,顯示  
  21.  * @author bicashy  
  22.  *  
  23.  */  
  24. public class Dom4jAPP   
  25. {   
  26.     /**  
  27.      * 寫(xiě)XML文件  
  28.      * @param path  
  29.      */  
  30.     public void writeXML(String path)   
  31.     {   
  32.         try  
  33.         {   
  34.             FileWriter out = new FileWriter(path);   
  35.             Document doc = createDocument();   
  36.             write(doc,path);   
  37.         } catch (IOException e)   
  38.         {   
  39.             // TODO Auto-generated catch block   
  40.             e.printStackTrace();   
  41.         }   
  42.     }   
  43.        
  44.     /**  
  45.      * 返回Document對(duì)象  
  46.      * @param path  
  47.      * @return  
  48.      */  
  49.     public Document readXML(String path)   
  50.     {   
  51.         Document doc = null;   
  52.         try  
  53.         {   
  54.             SAXReader reader = new SAXReader();   
  55.             doc = reader.read(path);   
  56.                
  57.         } catch (DocumentException e)   
  58.         {   
  59.             // TODO Auto-generated catch block   
  60.             e.printStackTrace();   
  61.         }   
  62.         return doc;   
  63.     }   
  64.        
  65.        
  66.     public void displayXML(String path)   
  67.     {   
  68.         Document doc = readXML(path);   
  69.         Element root = doc.getRootElement();   
  70.            
  71.         for(Iterator it = root.elementIterator();it.hasNext();)   
  72.         {   
  73.             Element e = (Element)it.next();   
  74.             System.out.println("【內(nèi)容=="+e.getText()+"】【  enjoy屬性=="+e.attributeValue("enjoy")+"】");   
  75.         }   
  76.            
  77.         //枚舉名稱(chēng)為"bicashy"的節(jié)點(diǎn)   
  78.         for ( Iterator i = root.elementIterator("bicashy"); i.hasNext();) {   
  79.            Element foo = (Element) i.next();   
  80.            System.out.println("【內(nèi)容=="+foo.getText()+"】【  enjoy屬性=="+foo.attributeValue("enjoy")+"】");   
  81.         }   
  82.            
  83.         // 枚舉"bicashy"節(jié)點(diǎn)下的屬性   
  84.         Element bicashy = root.element("bicashy");   
  85.         for ( Iterator i = bicashy.attributeIterator(); i.hasNext(); ) {   
  86.            Attribute attribute = (Attribute) i.next();   
  87.            System.out.println("屬性"+attribute.getName()+"=="+attribute.getText());   
  88.         }   
  89.   
  90.         //枚舉"child"節(jié)點(diǎn)下的屬性   
  91.         Element child = root.element("parent").element("child");   
  92.         for ( Iterator i = child.attributeIterator(); i.hasNext(); ) {   
  93.            Attribute attribute = (Attribute) i.next();   
  94.            System.out.println("屬性"+attribute.getName()+"=="+attribute.getText());   
  95.         }   
  96.     }   
  97.        
  98.     //修改xml文件(將“child”節(jié)點(diǎn)的屬性“姓名”的值改為 “金剛”,將內(nèi)容改為“我是誰(shuí)?”)   
  99.     public void modifyXML(String path)   
  100.     {   
  101.         try  
  102.         {   
  103.             SAXReader reader = new SAXReader();   
  104.             Document doc = reader.read(path);   
  105.             Element root = doc.getRootElement();   
  106.             Element child = root.element("parent").element("child");   
  107.             for ( Iterator i = child.attributeIterator(); i.hasNext(); )    
  108.             {   
  109.                    Attribute attribute = (Attribute) i.next();   
  110.                    if(attribute.getName().equals("姓名"))   
  111.                    {   
  112.                        attribute.setText("金剛");   
  113.                    }   
  114.             }   
  115.             child.setText("我是誰(shuí)?");   
  116.             write(doc,path);   
  117.                
  118.         } catch (DocumentException e)   
  119.         {   
  120.             // TODO Auto-generated catch block   
  121.             e.printStackTrace();   
  122.         }   
  123.     }   
  124.        
  125.     //遞歸   
  126.     public void treeWalk(String path)   
  127.     {   
  128.         Document doc = readXML(path);   
  129.         Element root = doc.getRootElement();   
  130.         treeWalk(root);   
  131.     }   
  132.        
  133.     public void treeWalk(Element element)   
  134.     {   
  135.            for (int i = 0, size = element.nodeCount(); i < size; i++)     {   
  136.                Node node = element.node(i);   
  137.                if (node instanceof Element) {   
  138.                   treeWalk((Element) node);   
  139.                } else    
  140.                {   
  141.                    System.out.println("##"+"=="+node.getText());   
  142.                }   
  143.            }   
  144.     }   
  145.   
  146.     /**  
  147.      * 寫(xiě)入文件  
  148.      * @param doc     文檔對(duì)象  
  149.      * @param outpath 文件路徑  
  150.      */  
  151.     public void write(Document doc,String outpath)   
  152.     {   
  153.         XMLWriter writer = null;   
  154.         try  
  155.         {   
  156.             writer = new XMLWriter(new FileOutputStream(new File(outpath)));   
  157.             writer.write(doc);   
  158.             writer.close();   
  159.                
  160.             //美化格式(優(yōu)美格式)   
  161.             OutputFormat format = OutputFormat.createPrettyPrint();// 定義文檔的格式為美化型(pretty)   
  162.             format.setEncoding("GBK");// 格式編碼為“gbk”   
  163.             writer = new XMLWriter(System.out,format );   
  164.             writer.write(doc);   
  165.             //縮進(jìn)格式     
  166. //          format = OutputFormat.createCompactFormat();   
  167. //          writer = new XMLWriter(System.out,format);   
  168. //          writer.write(doc);   
  169.         } catch (Exception e)   
  170.         {   
  171.             // TODO Auto-generated catch block   
  172.             e.printStackTrace();   
  173.         }   
  174.     }   
  175.        
  176.     /*  
  177.         <?xml version="1.0" encoding="GBK"?>  
  178.         <root>  
  179.           <bicashy name="wangpengfei" sex="male" enjoy="computer">Best of you</bicashy>  
  180.           <ladybird name="dibowey" sex="male" enjoy="muder">III kill</ladybird>  
  181.           <parent>  
  182.             <child 姓名="毛孩" 乳名="小屁孩">WHO AM I?</child>  
  183.           </parent>  
  184.         </root>   
  185.      */  
  186.     public Document createDocument()   
  187.     {   
  188.         Document document = DocumentHelper.createDocument();   
  189.         Element root = document.addElement("root");   
  190.            
  191.         Element bicashy1 =    
  192.               root.addElement("bicashy")   
  193.                   .addAttribute("name""wangpengfei")     
  194.                   .addAttribute("sex","male")   
  195.                   .addAttribute("enjoy""computer")   
  196.                   .addText("Best of you");   
  197.         Element bicashy2 =    
  198.               root.addElement("ladybird")   
  199.                   .addAttribute("name""dibowey")     
  200.                   .addAttribute("sex","male")   
  201.                   .addAttribute("enjoy""muder")   
  202.                   .addText("III kill");   
  203.         Element bicashy3 =    
  204.               root.addElement("parent");   
  205.         Element bicashy4 =    
  206.                 bicashy3.addElement("child")   
  207.                         .addAttribute("姓名""毛孩")      
  208.                         .addAttribute("乳名","小屁孩")   
  209.                         .addText("WHO AM I?");   
  210.         return document;   
  211.     }   
  212.        
  213.     public static void main(String[] args)   
  214.     {   
  215.         Dom4jAPP app = new Dom4jAPP();   
  216.         app.writeXML("e:/bicashy.xml");   
  217.         app.displayXML("e:/bicashy.xml");   
  218.         //app.modifyXML("e:/bicashy.xml");   
  219.     }   
  220. }  


dom4j是基于面向接口的樣式來(lái)實(shí)現(xiàn)處理xml文檔的,這種方法在面向?qū)ο蟮念I(lǐng)域里特別常見(jiàn)。使用的常用接口都封裝在org.dom4j包里,而怎樣讀取xml文檔呢?則是使用一系列的api函數(shù),這些常用方法都封裝在org.dom4j.io。

(1)從面向?qū)ο蟮慕嵌葋?lái)看,dom4j對(duì)XML的文檔結(jié)構(gòu)進(jìn)行了封裝,從一個(gè)文檔的角度來(lái)說(shuō)(我們不考慮內(nèi)存中的數(shù)據(jù)結(jié)構(gòu)的話(huà)),可以簡(jiǎn)單的把一個(gè)XML文檔就當(dāng)做一個(gè)org.dom4j.Document:

         我們現(xiàn)在使用dom4j的Document結(jié)構(gòu),而不是使用w3c的Document結(jié)構(gòu),讀取一個(gè)文件,在內(nèi)存中構(gòu)造一個(gè)Document結(jié)構(gòu):Document doc=new SAXReader().read(new File("...")); 就可以獲得一個(gè)Document結(jié)構(gòu)。注意,dom4j同時(shí)也對(duì)w3c的Document結(jié)構(gòu),SAX Event的支持,具體的使用可以參看docs/cookbook.html中的內(nèi)容。這里不一一介紹。

(2)獲得一個(gè)dom4j的Document結(jié)構(gòu)之后,從面向?qū)ο蟮慕嵌?,我們自然而然的想起的是XML中的element,dom4j根據(jù)這種想法對(duì)這些進(jìn)行了封裝org.dom4j.Element類(lèi)就是對(duì)這些進(jìn)行了封裝,從Document中獲得Element

         首先獲得一個(gè)root元素,Element root=doc.getRootElement();而其他的元素也可以根據(jù)這個(gè)root元素來(lái)獲得。

(4)一個(gè)元素的屬性的話(huà),則是Attribute,在Element類(lèi)中,有非常容易操作的方法添加于獲得Attribute,addAttritue();attribute()等函數(shù)原型都提供了操作。

(5)dom4j同時(shí)也提供了在內(nèi)存中的數(shù)據(jù)結(jié)構(gòu)的構(gòu)造,不降低處理的靈活性。如:Node就是對(duì)一個(gè)xml文檔樹(shù)的節(jié)點(diǎn)的封裝,而B(niǎo)ranch則是對(duì)一個(gè)樹(shù)的分支的封裝,通過(guò)使用這些,可以靈活的按照樹(shù)的結(jié)構(gòu)來(lái)處理xml文檔。敏感的朋友可能會(huì)注意到,Document和Element都是Node與Branch的子類(lèi),這種不同功能的領(lǐng)域的封裝可以按照對(duì)象的處理與數(shù)據(jù)結(jié)構(gòu)的結(jié)合,靈活的處理xml文檔。
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶(hù)發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
w3c parse xml
DOM4j使用教程
dom4j(Version 1.6.1)快速入門(mén)
XML文件操作之dom4j
Dom4j 學(xué)習(xí)筆記
Java程序員從笨鳥(niǎo)到菜鳥(niǎo)之(二十七)XML之Jdom和DOM4J解析
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服