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

          jdom 修改 xml

          Posted on 2009-07-29 23:37 Gavin.lee 閱讀(227) 評論(0)  編輯  收藏 所屬分類: xml doc 操作
          修改前:
          <?xml version="1.0" encoding="GBK"?> 
          <bookList> 
             
          <book> 
                 
          <name>Java</name> 
                 
          <author>張三</author> 
                 
          <publishDate>2002-6-6</publishDate> 
                 
          <price>35.0</price> 
             
          </book> 
             
          <book> 
                 
          <name>XML</name> 
                 
          <author>李四</author> 
                 
          <publishDate>2002-9-16</publishDate> 
                 
          <price>92.0</price> 
             
          </book> 
          </bookList>
          修改后:
          <?xml version="1.0" encoding="UTF-8"?>
          <bookList> 
             
          <book hot="true"> 
                 
          <name>Java</name> 
                 
          <author>redtroy</author> 
                 
          <publishDate>2002-6-6</publishDate> 
                 
          <price>100.0</price> 
             
          </book> 
             
          <book> 
                 
          <name>XML</name> 
                 
          <author>李四</author> 
                 
          <publishDate>2002-9-16</publishDate> 
                 
          <price>92.0</price> 
             
          </book> 
          </bookList>

          操作類:
          package com.Gavin.xml;

          import org.jdom.*;
          import org.jdom.output.*;
          import org.jdom.input.*;
          import java.io.*;
          import java.util.List;

          public class JDOMDemo {

              
          public static void main(String[] args) throws Exception {
                  SAXBuilder sb 
          = new SAXBuilder();
                  Document doc 
          = sb.build(new FileInputStream("d:/exampleA.xml"));
                  Element root 
          = doc.getRootElement();
                  List books 
          = root.getChildren();
                  Element book 
          = (Element) books.get(0);
                  Attribute newattribute 
          = new Attribute("hot""true");
                  book.setAttribute(newattribute);
                  Element author 
          = (Element) book.getChild("author");
                  author.setText(
          "redtroy");
                  Element price 
          = book.getChild("price");
                  price.setText(Float.toString(
          100));

                  Format format 
          = Format.getCompactFormat();
                  
          //設置文檔字符編碼
                  format.setEncoding("GB18030");
                  
          //設置縮進字符串
                  format.setIndent("  ");

          //        XMLOutputter out = new XMLOutputter();
                  XMLOutputter out = new XMLOutputter(format);
                  out.output(doc, 
          new FileOutputStream("d:/exampleB.xml"));
              }

          }
          主站蜘蛛池模板: 枞阳县| 临泉县| 永新县| 伊金霍洛旗| 株洲市| 德清县| 西青区| 隆林| 静宁县| 淮阳县| 和顺县| 东明县| 姚安县| 长岭县| 安乡县| 巧家县| 乌拉特中旗| 苏尼特右旗| 钟祥市| 梧州市| 剑河县| 乌兰浩特市| 阿拉善盟| 广河县| 疏勒县| 永平县| 泾阳县| 来安县| 教育| 深水埗区| 刚察县| 扎鲁特旗| 嘉荫县| 崇文区| 周宁县| 平顺县| 确山县| 丹巴县| 当涂县| 宾阳县| 喀喇沁旗|