軟件藝術(shù)思考者  
          混沌,彷徨,立志,蓄勢(shì)...
          公告
          日歷
          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導(dǎo)航

          隨筆分類(86)

          隨筆檔案(85)

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

           
          JDOM處理XML快速上手
          2006-09-11 16:42

          JDOM處理XML快速上手- -

          ??????????????????????????????????????

          ??? 在 JDOM 中,XML 元素就是 Element 的實(shí)例,XML 屬性就是 Attribute 的實(shí)例,XML 文檔本身就是 Document 的實(shí)例。
          ??? 因?yàn)?JDOM 對(duì)象就是像 Document、Element 和 Attribute 這些類的直接實(shí)例,因此創(chuàng)建一個(gè)新 JDOM 對(duì)象就如在 Java 語言中使用 new 操作符一樣容易。JDOM 的使用是直截了當(dāng)?shù)摹?br />??? JDOM 使用標(biāo)準(zhǔn)的 Java 編碼模式。只要有可能,它使用 Java new 操作符而不故弄玄虛使用復(fù)雜的工廠化模式,使對(duì)象操作即便對(duì)于初學(xué)用戶也很方便。
          ???
          ??? 本文分兩步對(duì)JDOM的應(yīng)用加以介紹:XML創(chuàng)建 和 XML解析
          一、XML文檔創(chuàng)建
          ??? 我們由零開始利用JDOM生成一個(gè)XML文檔。最后的結(jié)果(樣本文檔)看起來象這樣:
          ??? <?xml version="1.0" encoding="UTF-8"?>
          ??? <MyInfo comment="introduce myself">
          ??????? <name>kingwong</name>
          ??????? <sex value="male"/>
          ??????? <contact>
          ??????????? <telephone>87654321</telephone>
          ??????? </contact>
          ??? </MyInfo>
          ??? 1.以 MyInfo 為根元素創(chuàng)建文檔
          ??????? Element rootElement = new Element("MyInfo");//所有的XML元素都是 Element 的實(shí)例。根元素也不例外:)
          ??????? Document myDocument = new Document(rootElement);//以根元素作為參數(shù)創(chuàng)建Document對(duì)象。一個(gè)Document只有一個(gè)根,即root元素。
          ??? 2.給根元素添加屬性
          ??????? Attribute rootAttri = new Attribute("comment","introduce myself");//創(chuàng)建名為 commnet,值為 introduce myself 的屬性。
          ??????? rootElement.setAttribute(rootAttri);//將剛創(chuàng)建的屬性添加到根元素。
          ??????? 這兩行代碼你也可以合成一行來寫,象這樣:
          ??????? rootElement.setAttribute(new Attribute("comment","introduce myself"));
          ??????? 或者
          ??????? rootElement.setAttribute("comment","introduce myself");
          ??? 3.添加元素和子元素
          ??????? JDOM里子元素是作為 content(內(nèi)容)添加到父元素里面去的,所謂content就是類似上面樣本文檔中<name></name>之間的東東,即kingwong。羅嗦了點(diǎn)是吧:)
          ??????? Element nameElement = new Element("name");//創(chuàng)建 name 元素
          ??????? nameElement.addContent("kingwong");//將kingwong作為content添加到name元素
          ?rootElement.addContent(nameElement);//將name元素作為content添加到根元素
          ?
          ?這三行你也可以合為一句,象這樣:
          ?rootElement.addContent((Content)(new Element("name").addContent("kingwong")));//因?yàn)閍ddContent(Content child)方法返回的是一個(gè)Parent接口,而Element類同時(shí)繼承了Content類和實(shí)現(xiàn)了Parent接口,所以我們把它造型成Content。
          ?
          ??????? 我們用同樣的方法添加帶屬性的子元素<sex value="male"/>
          ??????? rootElement.addContent(new Element("sex").setAttribute("value","male"));//注意這里不需要轉(zhuǎn)型,因?yàn)閍ddAttribute(String name,String value)返回值就是一個(gè) Element。
          ???????
          ??????? 同樣的,我們添加<contract />元素到根元素下,用法上一樣,只是稍微復(fù)雜了一些:
          ??????? rootElement.addContent((Content)(new Element("contact").addContent((Content)(new Element("telephone").addContent("87654321")))));
          ??????? 如果你對(duì)這種簡寫形式還不太習(xí)慣,你完全可以分步來做,就象本節(jié)剛開始的時(shí)候一樣。事實(shí)上如果層次比較多,寫成分步的形式更清晰些,也不容易出錯(cuò)。
          ??? 4.刪除子元素
          ??????? 這個(gè)操作比較簡單:
          ??????? rootElement.removeChild("sex");//該方法返回一個(gè)布爾值
          ???????
          ??????? 到目前為止,我們學(xué)習(xí)了一下JDOM文檔生成操作。上面建立了一個(gè)樣本文檔,可是我們?cè)趺粗缹?duì)不對(duì)呢?因此需要輸出來看一下。我們將JDOM生成的文檔輸出到控制臺(tái),使用 JDOM 的 XMLOutputter 類。
          ??? 5.? 將 JDOM 轉(zhuǎn)化為 XML 文本
          ??????? XMLOutputter xmlOut = new XMLOutputter("? ",true);
          ?try {
          ??xmlOut.output(myDocument,System.out);
          ?} catch (IOException e) {
          ??e.printStackTrace();
          ?}
          ?XMLOutputter 有幾個(gè)格式選項(xiàng)。這里我們已指定希望子元素從父元素縮進(jìn)兩個(gè)空格,并且希望元素間有空行。
          ?new XMLOutputter(java.lang.String indent, boolean newlines)這個(gè)方法在最新版本中已經(jīng)不建議使用。JDOM有一個(gè)專門的用來定義格式化輸出的類:org.jdom.output.Format,如果你沒有特殊的要求,有時(shí)候使用里面的幾個(gè)靜態(tài)方法(應(yīng)該可以說是預(yù)定義格式)如 getPrettyFormat()就可以了。我們把上面的輸出格式稍微改一下,就象這樣:
          ?XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());?
          ??? 6.將JDOM文檔轉(zhuǎn)化為其他形式
          ??????? XMLOutputter 還可輸出到 Writer 或 OutputStream。為了輸出JDOM文檔到一個(gè)文本文件,我們可以這樣做:
          ??????? FileWriter writer = new FileWriter("/some/directory/myFile.xml");
          ??????? outputter.output(myDocument, writer);
          ??????? writer.close();
          ???????
          ??????? XMLOutputter 還可輸出到字符串,以便程序后面進(jìn)行再處理:
          ??????? Strng outString = xmlOut.outputString(myDocument);
          ???????
          ??????? 當(dāng)然,在輸出的時(shí)候你不一定要輸出所有的整個(gè)文檔,你可以選擇元素進(jìn)行輸出:
          ??????? xmlOut.output(rootElement.getChild("name"),System.out);
          ??????? 一句話,JDOM非常靈活方便!如果你想進(jìn)一步研究JDOM,請(qǐng)到官方網(wǎng)站去看一看:http://www.jdom.org

          ??? 本節(jié)示例源碼:
          package com.cyberobject.study;

          import java.io.IOException;

          import org.jdom.Attribute;
          import org.jdom.Content;
          import org.jdom.Document;
          import org.jdom.Element;
          import org.jdom.output.Format;
          import org.jdom.output.XMLOutputter;

          /**
          ?* @author kingwong
          ?*
          ?* TODO To change the template for this generated type comment go to
          ?* Window - Preferences - Java - Code Style - Code Templates
          ?*/
          public class TestJDOM {

          ?public static void main(String[] args)
          ?{
          ??Element rootElement = new Element("MyInfo");
          ??Document myDocument = new Document(rootElement);
          ??
          //??Attribute rootAttri = new Attribute("comment","introduce myself");
          //??rootElement.setAttribute(rootAttri);
          ??
          ??rootElement.setAttribute("comment","introduce myself");
          ??//rootElement.setAttribute(new Attribute("comment","introduce myself"));
          //??Element sexElement = new Element("sex");
          //??rootElement.addContent(sexElement);
          ??
          //??Element nameElement = new Element("name");
          //??nameElement.addContent("kingwong");
          //??rootElement.addContent(nameElement);
          ??
          ??rootElement.addContent((Content)(new Element("name").addContent("kingwong")));
          ??rootElement.addContent(new Element("sex").setAttribute("value","male"));
          ??rootElement.addContent((Content)(new Element("contract").addContent((Content)(new Element("telephone").addContent("87654321")))));
          ??
          ??rootElement.removeChild("sex");
          ??
          ??XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
          ??try {
          ???xmlOut.output(myDocument,System.out);
          ???//xmlOut.output(rootElement.getChild("name"),System.out);
          ???//String outString = xmlOut.outputString(myDocument);
          ??} catch (IOException e) {
          ???e.printStackTrace();
          ??}
          ?}
          }

          ???????
          二、XML文檔解析
          ??? JDOM 不光可以很方便的建立XML文檔,它的另一個(gè)用處是它能夠讀取并操作現(xiàn)有的 XML 數(shù)據(jù)。
          ??? JDOM的解析器在org.jdom.input.*這個(gè)包里,其中的DOMBuilder的功能是將DOM模型的Document解析成JDOM模型的Document;SAXBuilder的功能是從文件或流中解析出符合JDOM模型的XML樹。由于我們經(jīng)常要從一個(gè)文件里讀取數(shù)據(jù),因此我們應(yīng)該采用后者作為解析工具。
          解析一個(gè)xml文檔,基本可以看成以下幾個(gè)步驟:
          ??? 1.實(shí)例化一個(gè)合適的解析器對(duì)象
          ??????? 本例中我們使用SAXBuilder:
          ??????? SAXBuilder sb = new SAXBuilder();
          ??? 2.以包含XML數(shù)據(jù)的文件為參數(shù),構(gòu)建一個(gè)文檔對(duì)象myDocument
          ??????? Document myDocument = sb.build(/some/directory/myFile.xml);
          ??? 3.獲到根元素
          ??????? Element rootElement = myDocument.getRootElement();
          ???????
          ??????? 一旦你獲取了根元素,你就可以很方便地對(duì)它下面的子元素進(jìn)行操作了,下面對(duì)Element對(duì)象的一些常用方法作一下簡單說明:
          ??????? getChild("childname") 返回指定名字的子節(jié)點(diǎn),如果同一級(jí)有多個(gè)同名子節(jié)點(diǎn),則只返回第一個(gè);如果沒有返回null值。
          ??????? getChildren("childname") 返回指定名字的子節(jié)點(diǎn)List集合。這樣你就可以遍歷所有的同一級(jí)同名子節(jié)點(diǎn)。
          ??????? getAttributeValue("name") 返回指定屬性名字的值。如果沒有該屬性則返回null,有該屬性但是值為空,則返回空字符串。
          ??????? getChildText("childname") 返回指定子節(jié)點(diǎn)的內(nèi)容文本值。
          ??????? getText() 返回該元素的內(nèi)容文本值。
          ???????
          ??????? 還有其他沒有羅列出來的方法,如果需要的話,可以隨時(shí)查閱JDOM的在線文檔:http://www.jdom.org/docs/apidocs/index.html。當(dāng)然你可以在你需要的地方添加、刪除元素操作,還記得上面的創(chuàng)建XML的方法嗎?呵呵~~~
          ???????
          ??????? 學(xué)習(xí)新東東還是從實(shí)例學(xué)起最為快捷,下面簡單舉個(gè)例子,就以上面的XML樣本代碼來學(xué)習(xí)JDOM的XML解析。本例中讀取了樣本XML文件里一些屬性和content,最后我們還在contact元素里插入了一個(gè)新元素<email value="wanghua@cyberobject.com" />。盡管我們實(shí)現(xiàn)了對(duì)于XML的基本操作,細(xì)心的朋友可能會(huì)
          有疑問:如果XML文檔的層次稍微復(fù)雜一些,如果嵌套多達(dá)幾十上百層的話(開個(gè)玩笑),如果靠這樣從根元素一級(jí)一級(jí)地通過getChild("childname")來訪問子元素的話,將會(huì)非常痛苦!是的,的確是這樣,但是我們有另一個(gè)有力的工具XPath,為什么不用呢?這是后話!先賣個(gè)關(guān)子(手敲累啦,下回吧,呵呵)。
          ???????
          /*
          ?* Created on 2004-8-21
          ?*
          ?* TODO To change the template for this generated file go to
          ?* Window - Preferences - Java - Code Style - Code Templates
          ?*/
          package com.cyberobject.study;

          import org.jdom.Document;
          import org.jdom.Element;
          import org.jdom.input.SAXBuilder;
          import org.jdom.output.Format;
          import org.jdom.output.XMLOutputter;

          /**
          ?* @author kingwong
          ?*
          ?* TODO To change the template for this generated type comment go to
          ?* Window - Preferences - Java - Code Style - Code Templates
          ?*/
          public class TestJDOM2 {
          ?public static void main(String[] args){
          ?SAXBuilder sb = new SAXBuilder();
          ??? try
          ??? {???????
          ???? Document doc = sb.build("myFile.xml");
          ??Element root = doc.getRootElement();
          ??
          ??String str1 = root.getAttributeValue("comment");
          ??System.out.println("Root Element's comment attribute is : " + str1);
          ??String str2 = root.getChild("sex").getAttributeValue("value");
          ??System.out.println("sex Element's value attribute is : " + str2);
          ??String str3 = root.getChildText("name");
          ??System.out.println("name Element's content is :" + str3);
          ??String str4 = root.getChild("contact").getChildText("telephone");
          ??System.out.println("contact Element's telephone subelement content is : " + str4 + "\n");
          ??Element inputElement = root.getChild("contact");?
          ??inputElement.addContent(new Element("email").setAttribute("value","wanghua@cyberobject.com"));
          ??
          ??XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
          ???? String outStr = xmlOut.outputString(root);
          ???? System.out.println(outStr);
          ??? }
          ??? catch(Exception e)
          ??? {
          ??????? e.printStackTrace();
          ??? }
          }
          }
          ?????

          posted on 2006-11-30 12:38 智者無疆 閱讀(524) 評(píng)論(4)  編輯  收藏 所屬分類: about java
          評(píng)論:
          • # re: JDOM學(xué)習(xí) 相關(guān)知識(shí)1  self Posted @ 2006-11-30 15:47
            http://www.zvon.org/xxl/XPathTutorial/General_chi/examples.html  回復(fù)  更多評(píng)論   

          • # re: JDOM學(xué)習(xí)  self Posted @ 2006-11-30 16:57
            一個(gè)writeXml.java文件。
            package com.utral.zlj.testClasses;
            import java.io.FileWriter;
            import java.io.IOException;

            import org.jdom.*;
            import org.jdom.output.Format;
            import org.jdom.output.XMLOutputter;
            public class writeXml {
            /*試驗(yàn)類,任務(wù):
            1.新建xml文件,
            2.向xml文件寫入數(shù)據(jù)
            by zhanglijun 2006-11-30
            */
            public static void main(String[] args) {

            Element root = new Element("myBooks"); //創(chuàng)建一個(gè)根元素
            Document doc=new Document(root);//以根元素創(chuàng)建一個(gè)文檔

            Element Item=new Element("book");
            Element book1= new Element("bookname");//添加子節(jié)點(diǎn)元素
            book1.addContent("計(jì)算機(jī)應(yīng)用");
            book1.setAttribute(new Attribute("price","23.5")) ;//添加一個(gè)價(jià)格屬性
            Item.addContent(book1);
            Element author= new Element("bookAuthor");
            author.addContent("張立軍");
            author.setAttribute("telephone","123456");
            Item.addContent(author);
            root.addContent(Item);
            Element Item2=new Element("book");

            Element book2=new Element("bookname");
            book2.addContent("數(shù)據(jù)庫原理");
            book2.setAttribute("price","30");
            Item2.addContent(book2);
            Element author2=new Element("bookAuthor").addContent("盧竹");
            author2.setAttribute("telephone","888888");
            Item2.addContent(author2);
            root.addContent(Item2);
            //將xml對(duì)象轉(zhuǎn)換成xml文本
            XMLOutputter xmlouter=new XMLOutputter(Format.getPrettyFormat().setEncoding("gb2312"));
            try {
            xmlouter.output(doc,System.out);
            } catch (IOException e) {
            e.printStackTrace();
            }

            try {
            FileWriter writer= new FileWriter("C://test.xml");
            xmlouter.output(doc,writer);
            writer.close();
            } catch (IOException e) {
            e.printStackTrace();
            }
            }



            }
            寫入后的文件內(nèi)容:<?xml version="1.0" encoding="gb2312"?>
            <myBooks>
            <book>
            <bookname price="23.5">計(jì)算機(jī)應(yīng)用</bookname>
            <bookAuthor telephone="123456">張立軍</bookAuthor>
            </book>
            <book>
            <bookname price="30">數(shù)據(jù)庫原理</bookname>
            <bookAuthor telephone="888888">盧竹</bookAuthor>
            </book>
            </myBooks>

              回復(fù)  更多評(píng)論   

          • # re: JDOM學(xué)習(xí)  self Posted @ 2006-11-30 16:58
            一個(gè)readXml.java的文件。package com.utral.zlj.testClasses;
            /**
            * 1.從xml文件中讀取數(shù)據(jù)
            * 2.對(duì)根節(jié)點(diǎn)的名字進(jìn)行修改,其他節(jié)點(diǎn)的修改方法:得到那個(gè)節(jié)點(diǎn)后用重構(gòu)的方法,然后寫入。
            * by zhanglijun 2006-11-30
            * ***/
            import java.io.FileWriter;
            import java.io.IOException;
            import java.util.List;

            import org.jdom.*;
            import org.jdom.input.*;
            import org.jdom.output.Format;
            import org.jdom.output.XMLOutputter;
            import org.jdom.xpath.XPath;

            import sun.security.krb5.internal.crypto.x;
            public class readXml {

            public static void main(String[] args) {
            SAXBuilder sa=new SAXBuilder();

            try {
            Document doc = sa.build("C:\\test.xml");//建立一個(gè)文檔對(duì)象
            Element root =doc.getRootElement();//得到根元素
            root.setName("試驗(yàn)");
            List list = root.getChildren();
            for(int i=0;i<list.size();i++){
            Element node=(Element)list.get(i);
            Element book=node.getChild("bookname");
            System.out.print("書名:"+book.getText()+",價(jià)格:"+book.getAttributeValue("price"));
            Element authornode=(Element)node.getChild("bookAuthor");
            System.out.println("作者:"+authornode.getText()+",電話:"+authornode.getAttributeValue("telephone")+".");

            }
            XMLOutputter xmlouter=new XMLOutputter(Format.getPrettyFormat().setEncoding("gb2312"));
            FileWriter file=new FileWriter("C://test.xml");
            xmlouter.output(doc,file);
            } catch (JDOMException e) {
            e.printStackTrace();
            } catch (IOException e) {
            e.printStackTrace();
            }
            }
            }
            運(yùn)行后的結(jié)果為:
            書名:計(jì)算機(jī)應(yīng)用,價(jià)格:23.5作者:張立軍,電話:123456.
            書名:數(shù)據(jù)庫原理,價(jià)格:30作者:盧竹,電話:888888.  回復(fù)  更多評(píng)論   

          • # re: JDOM學(xué)習(xí)  天際 Posted @ 2008-09-24 11:26
            超好的文章  回復(fù)  更多評(píng)論   

           
          Copyright © 智者無疆 Powered by: 博客園 模板提供:滬江博客


             觀音菩薩贊

          主站蜘蛛池模板: 杭锦后旗| 咸丰县| 本溪| 集安市| 镇原县| 山西省| 砀山县| 辽中县| 启东市| 多伦县| 昂仁县| 贵州省| 上饶市| 武安市| 温宿县| 蓬莱市| 阿拉善左旗| 凌海市| 陇川县| 舒城县| 石棉县| 浦东新区| 麻栗坡县| 西贡区| 宿松县| 桂阳县| 英吉沙县| 盖州市| 连城县| 百色市| 肃北| 军事| 普兰店市| 抚顺市| 黑山县| 太湖县| 河西区| 中超| 沐川县| 海晏县| 金阳县|