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

          第3篇,嘗試使用JDOM

          Posted on 2006-11-24 21:10 Zou Ang 閱讀(233) 評論(0)  編輯  收藏 所屬分類:
          今天晚上在宿舍看那本《Java Web服務高級教程》,剛開始講的是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才行
          主站蜘蛛池模板: 霍州市| 鲁山县| 历史| 庄浪县| 额尔古纳市| 霸州市| 台东市| 安达市| 宁城县| 濉溪县| 平阴县| 聂拉木县| 陵水| 武义县| 深圳市| 彭山县| 潢川县| 广丰县| 曲周县| 汉中市| 岑巩县| 余干县| 临汾市| 巴南区| 和顺县| 景泰县| 永兴县| 深泽县| 宜昌市| 民丰县| 西平县| 东乡族自治县| 仁布县| 高邮市| 宁强县| 牟定县| 游戏| 宁城县| 陈巴尔虎旗| 平阴县| 南通市|