溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          雪山飛鵠

          溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks
          package com.dom;

          import java.io.File;
          import java.util.List;

          import javax.xml.parsers.DocumentBuilder;
          import javax.xml.parsers.DocumentBuilderFactory;
          import javax.xml.transform.OutputKeys;
          import javax.xml.transform.Transformer;
          import javax.xml.transform.TransformerFactory;
          import javax.xml.transform.dom.DOMSource;
          import javax.xml.transform.stream.StreamResult;

          import org.w3c.dom.Document;
          import org.w3c.dom.Element;

          public class DomCreateXML {

              
          public static void main(String[] args) throws Exception{
                  DocumentBuilder builder
          =DocumentBuilderFactory.newInstance().newDocumentBuilder();
                  Document document 
          = builder.newDocument();
                  Element rootElement
          =document.createElement("root");
                  
                  
          for (int i = 0; i < 100; i++) {
                      Element bookElement
          =document.createElement("book");
                      bookElement.setAttribute(
          "id", (i+1)+"");
                      
                      Element nameElement
          =document.createElement("name");
                      nameElement.setTextContent(
          "Spring");
                      
                      Element priceElement
          =document.createElement("price");
                      priceElement.setTextContent(
          "88");
                      
                      Element authorElement
          =document.createElement("author");
                      authorElement.setTextContent(
          "李剛");
                      
                      rootElement.appendChild(bookElement);
                      bookElement.appendChild(nameElement);
                      bookElement.appendChild(priceElement);
                      bookElement.appendChild(authorElement);
                  }
                  
                  document.appendChild(rootElement);
                  
                  TransformerFactory factory
          =TransformerFactory.newInstance();
                  Transformer transformer
          =factory.newTransformer();
                  transformer.setOutputProperty(OutputKeys.ENCODING, 
          "utf-8");
                  transformer.transform(
          new DOMSource(document), new StreamResult(new File("d:/1.xml")));
              }

          }
          生成的xml
          <?xml version="1.0" encoding="utf-8" standalone="no"?>
          <root>
              
          <book id="1">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="2">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="3">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="4">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="5">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="6">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="7">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="8">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="9">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="10">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="11">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="12">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="13">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="14">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="15">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="16">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="17">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="18">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="19">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="20">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="21">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="22">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="23">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="24">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="25">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="26">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="27">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="28">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="29">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="30">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="31">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="32">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="33">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="34">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="35">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="36">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="37">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="38">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="39">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="40">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="41">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="42">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="43">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="44">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="45">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="46">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="47">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="48">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="49">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="50">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="51">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="52">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="53">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="54">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="55">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="56">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="57">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="58">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="59">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="60">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="61">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="62">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="63">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="64">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="65">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="66">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="67">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="68">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="69">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="70">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="71">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="72">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="73">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="74">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="75">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="76">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="77">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="78">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="79">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="80">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="81">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="82">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="83">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="84">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="85">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="86">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="87">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="88">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="89">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="90">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="91">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="92">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="93">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="94">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="95">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="96">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="97">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="98">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="99">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
              
          <book id="100">
                  
          <name>Spring</name>
                  
          <price>88</price>
                  
          <author>李剛</author>
              
          </book>
          </root>
          posted on 2011-08-24 11:29 雪山飛鵠 閱讀(473) 評論(0)  編輯  收藏 所屬分類: javaseandroid
          主站蜘蛛池模板: 石屏县| 昆明市| 区。| 威宁| 搜索| 阳谷县| 济南市| 台北市| 常熟市| 垦利县| 温宿县| 吉木萨尔县| 黑山县| 嘉兴市| 诸城市| 深圳市| 韶山市| 临朐县| 饶河县| 昆山市| 通海县| 六盘水市| 古浪县| 巫山县| 浦北县| 汉寿县| 庄河市| 攀枝花市| 会同县| 北川| 婺源县| 沧州市| 烟台市| 曲靖市| 宁南县| 广安市| 玛沁县| 桃园县| 称多县| 南开区| 达拉特旗|