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

          日歷

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          隨筆分類

          隨筆檔案(1)

          收藏夾(18)

          搜索

          •  

          最新評論

          用jdom操縱xml文檔例子

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

          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();
            //新建根節點
            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");
            //將子節點加入到父節點
            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)
            {}
           }
          }


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


          網站導航:
           
          主站蜘蛛池模板: 上蔡县| 茌平县| 南靖县| 吉隆县| 台江县| 呼玛县| 安顺市| 满城县| 日照市| 革吉县| 黑龙江省| 阿合奇县| 安义县| 沂源县| 县级市| 温泉县| 柞水县| 静乐县| 旅游| 马尔康县| 祥云县| 隆昌县| 枞阳县| 无锡市| 白城市| 丁青县| 封丘县| 定襄县| 和静县| 如东县| 大荔县| 禹城市| 西华县| 永登县| 张家港市| 双桥区| 哈密市| 舟山市| 扶沟县| 砚山县| 平凉市|