轻松使用dom创建xml
2012-04-08 08:39
357 查看
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>
相关文章推荐
- 使用PHP DOM-XML创建和解析XML文件
- 使用XML的DOM和XPath来创建多项选择题的在线测试
- Java 使用DOM创建和读取XML
- 使用DOM创建和写入xml文件
- 使用XML的DOM和XPath来创建多项选择题的在线测试
- [Qt教程] 第28篇 XML(二)使用DOM创建和操作XML文档
- 如何使用PHP DOM创建动态的XML文件
- 使用DOM创建XML
- XML 问题: 超越DOM(轻松使用 DOM 的技巧和诀窍)
- 使用xml.dom.minidom创建并写入xml文件
- 使用PHP DOM-XML创建和解析XML文件
- XML 问题: 超越DOM(轻松使用 DOM 的技巧和诀窍)
- php4/5使用Dom方式读取和解析和创建XML简单教程
- 如何使用PHP DOM创建动态的XML文件
- java29.XML操作------使用java DOM方式创建XML数据
- XML&nbsp;问题:&nbsp;超越DOM(轻松使用&nbsp;DOM&nbsp;的技巧和诀窍)
- php使用 DOM 创建xml文档
- 使用xml.dom.minidom创建xml
- 使用DOM创建和操作XML文档
- 使用DOM创建XML