yanmin

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            16 隨筆 :: 1 文章 :: 9 評論 :: 0 Trackbacks

          JDOM 用來處理XML,在編程中應用。通過JDOM能得到XML中的信息。

          //exampleA.xml
                      <?xml version="1.0" encoding="GBK"?>
                      <bookList>
                         <book>
                             <name>Java編程</name>
                             <author>張三</author>
                             <publishDate>2005-10-28</publishDate>
                             <price>55.0</price>
                         </book>
                         <book>
                             <name>J2EE開發應用</name>
                             <author>李四</author>
                             <publishDate>2005-9-16</publishDate>
                             <price>100.0</price>
                         </book>
                      </bookList>

                      
                      
                      ---------------------------------------------------
                      import org.jdom.*;
                      import org.jdom.input.*;
                      import org.jdom.output.*;
                      import java.io.*;
                      public class cute{

                      public static void main(String args[]){
                      try{

                      org.jdom.input.SAXBuilder sb=new org.jdom.input.SAXBuilder();
                      //創建文檔
                      org.jdom.Document    doc=sb.build(new
                      FileInputStream("c:\\example.xml");
                      //加入一條處理指令
                      org.jdom.ProcessingInstruction pi = new
                      ProcessingInstruction("xml-stylesheet","href=\"bookList.html.xsl\"
                      type=\"text/xsl\"";
                      //把這條處理指令,加入文檔中
                      doc.addContent(pi);
                      //獲得這個文檔得跟元素
                      org.jdom.Element el=doc.getRootElement();
                      //獲得這個跟元素,的所有子元素
                      java.util.List ls=el.getChildren();
                      //得到第一個子元素
                      org.jdom.Element book=(Element)ls.get(0);
                      //給這個字元素添加一條屬性
                      org.jdom.Attribute attr=new Attribute("hot","true";
                      book.setAttribute(attr);

                      //獲得這個元素的子元素,(指定)
                      org.jdom.Element el2=book.getChild("author";
                      //輸出這個元素的值
                      System.out.println(el2.getName());
                      //給這個元素的值改個名字
                      el2.setText("cute";

                      //再指定元素獲得這個值
                      org.jdom.Element el3=book.getChild("price";
                      //給這個值換個值
                      el3.setText(Float.toString(50.0f));
                      String a="";
                      boolean bool=true;

                      org.jdom.output.XMLOutputter xml= new
                      org.jdom.output.XMLOutputter(a,bool,"gb2312";
                      xml.output(doc,new FileOutputStream("c:\\cute.xml");

                      }catch(Exception e){
                      System.out.println(e.getMessage());

                      }

           

                      }
                      }


          posted on 2005-10-29 14:10 yanmin 閱讀(144) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 什邡市| 连江县| 射洪县| 石渠县| 汉阴县| 扎兰屯市| 杂多县| 周口市| 彭泽县| 扎鲁特旗| 秦安县| 武夷山市| 新乡县| 太白县| 仁布县| 海晏县| 泾阳县| 桦南县| 宝坻区| 迁安市| 白银市| 桂林市| 南靖县| 德惠市| 福贡县| 连江县| 独山县| 临安市| 拉萨市| 台前县| 天柱县| 宜兴市| 商南县| 石林| 岳阳县| 涪陵区| 德钦县| 新龙县| 万盛区| 都江堰市| 晋城|