posts - 32, comments - 153, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          第3篇,嘗試使用JDOM

          Posted on 2006-11-24 21:10 Zou Ang 閱讀(243) 評論(0)  編輯  收藏 所屬分類:
          今天晚上在宿舍看那本《Java Web服務(wù)高級教程》,剛開始講的是xml,所以試著用JDOM來讀了一個自己寫的xml文檔:
          MyXml.xml
          <?xml?version="1.0"?encoding="UTF-8"?>
          <directory>
          ????
          <file?filename?=?"book.xml">
          ????????
          <description>A?book?list</description>
          ????
          </file>
          ????
          ????
          <file?filename="funny.jpg">
          ????????
          <description>A?funny?picture</description>
          ????
          </file>
          </directory>

          測試類
          package?learn.xml;

          import?java.io.File;
          import?java.io.IOException;
          import?java.io.StringWriter;
          import?java.io.Writer;
          import?java.util.List;

          import?org.jdom.Document;
          import?org.jdom.Element;
          import?org.jdom.JDOMException;
          import?org.jdom.input.SAXBuilder;
          import?org.jdom.output.Format;
          import?org.jdom.output.XMLOutputter;
          import?org.xml.sax.SAXException;

          /**
          ?*?
          ?*?2006-11-24
          ?*?
          ?*?
          @author?Zou?Ang?Contact?<a?href?="mailto:richardeee@gmail.com">Zou?Ang</a>
          ?
          */

          public?class?ParseXML?{
          ????
          public?void?testParse()?throws?IOException{
          ????????SAXBuilder?builder?
          =?new?SAXBuilder(false);
          //????????File?file?=?new?File("D:/XWL/AjaxLearning/WebContent/WEB-INF/dwr.xml");
          ????????try?{
          ????????????Document?document?
          =?builder.build(new?File(
          ????????????????????
          "D:/XWL/AjaxLearning/WebContent/WEB-INF/MyXml.xml"));
          ????????????Element?root?
          =?document.getRootElement();
          ????????????List
          <Element>?children?=?root.getChildren();
          ????????????System.out.println(
          "Number?of?childern:?"?+?children.size());
          ????????????
          for(Element?el?:?children){
          ????????????????String?str?
          =?el.getChildText("description");
          ????????????????System.out.println(str);
          ????????????}

          ????????}
          ?catch?(JDOMException?e)?{
          ????????????e.printStackTrace();
          ????????}

          ????}

          ????
          ????
          public?void?createXML()throws?IOException,JDOMException{
          //????????SAXBuilder?builder?=?new?SAXBuilder(false);
          ????????Element?root?=?new?Element("MyMessage");
          ????????Document?document?
          =?new?Document(root);
          ????????Element?message?
          =?new?Element("message");
          ????????message.setAttribute(
          "type",?"text");
          ????????message.setContent(
          new?Element("content").addContent("First?Message"));
          ????????root.addContent(message);
          ????????XMLOutputter?outputter?
          =?new?XMLOutputter(Format.getPrettyFormat());
          ????????outputter.output(document,?System.out);
          //????????root.
          ????}

          ????
          public?static?void?main(String?arg[])?throws?IOException,JDOMException{
          ????????ParseXML?pxml?
          =?new?ParseXML();
          ????????pxml.testParse();
          ????????pxml.createXML();
          ????}

          }


          輸出:
          Number?of?childern:? 2
          A?book?list
          A?funny?picture

          <? xml?version = " 1.0 " ?encoding = " UTF-8 " ?>
          < MyMessage >
          ??
          < message?type = " text " >
          ????
          < content > First?Message </ content >
          ??
          </ message >
          </ MyMessage >

          要盡快熟練使用JDOM才行
          主站蜘蛛池模板: 胶南市| 阜城县| 东乌珠穆沁旗| 徐州市| 巴彦淖尔市| 沙雅县| 永安市| 蕉岭县| 阿克苏市| 邵阳县| 武鸣县| 刚察县| 隆林| 阿拉善右旗| 广丰县| 夏津县| 云霄县| 淳安县| 新竹市| 乌审旗| 日土县| 盐津县| 襄城县| 鄂托克旗| 旬邑县| 清丰县| 桑日县| 宁远县| 敖汉旗| 江门市| 墨竹工卡县| 梁山县| 瓮安县| 清河县| 亳州市| 修水县| 肥城市| 五华县| 大埔县| 札达县| 西昌市|