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

          用jdom操縱xml文檔例子

          Posted on 2007-05-27 11:03 semovy 閱讀(272) 評論(0)  編輯  收藏 所屬分類: XML相關(guān)

          package com.semovy.test;

          import java.io.File;
          import java.io.FileOutputStream;
          import java.io.IOException;

          import org.jdom.Document;
          import org.jdom.Element;
          import org.jdom.output.XMLOutputter;

          public class Test {

           /**
            * @param args
            */
           public static void main(String[] args) {
            // TODO Auto-generated method stub
            //System.out.print(new MD5().getMD5ofStr(""));
            sampleJDom();
           }
             /*
              * jdom
              */
           public static void sampleJDom()
           {
            //新建文檔對象
            Document doc = new Document();
            //新建根節(jié)點
            Element book = new Element("book");
            book.setAttribute("title", "Programming with Ajax");
            Element chapter1 = new Element("chapter");
            chapter1.setAttribute("name", "Chapter1 About Ajax");
            Element chapter2 = new Element("chapter");
            chapter2.setAttribute("name", "Chapter2 B/S Structure");
            Element section1 = new Element("section");
            section1.addContent("What is Ajax?");
            Element section2 = new Element("section");
            section2.addContent("Definiens of Ajax");
            Element section3 = new Element("section");
            section3.addContent("HTTP Protocal");
            Element section4 = new Element("section");
            section4.addContent("Web Design");
            //將子節(jié)點加入到父節(jié)點
            chapter1.addContent(section1);
            chapter1.addContent(section2);
            chapter2.addContent(section3);
            chapter2.addContent(section4);
            book.addContent(chapter1);
            book.addContent(chapter2);
            doc.addContent(book);
            //將文檔輸出到控制臺
            XMLOutputter outputter = new XMLOutputter();
            try
            {
             outputter.output(doc, new FileOutputStream(new File("c:/a.xml")));
             System.out.println("輸出到文件成功!");
             outputter.output(doc, System.out);
             System.out.println("輸出到控制臺!");
            }catch(IOException ex)
            {}
           }
          }


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 宁城县| 宾川县| 双流县| 金山区| 盐山县| 安顺市| 沂源县| 榆中县| 牡丹江市| 达州市| 呼玛县| 舟山市| 东莞市| 怀仁县| 梨树县| 建宁县| 太和县| 玉林市| 丁青县| 贞丰县| 毕节市| 南投县| 罗城| 靖州| 马关县| 台前县| 绥中县| 砀山县| 客服| 清涧县| 泰安市| 营口市| 亚东县| 宝丰县| 苏尼特右旗| 家居| 桦甸市| 仁寿县| 定州市| 嘉祥县| 得荣县|