Chinaunix首页 | 论坛 | 博客
  • 博客访问: 30465492
  • 博文数量: 708
  • 博客积分: 12163
  • 博客等级: 上将
  • 技术积分: 8240
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-04 20:59
文章分类

全部博文(708)

分类: Java

2008-06-27 10:20:43

import java.io.File;
import java.io.FileOutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
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 CreateXMLDocument{

public static void main(String args[]) throws ParserConfigurationException, Exception{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.newDocument();
Element root = doc.createElement("data");
doc.appendChild(root);
root.setAttribute("type", "abc");


//创建文件

File file = new File("d:/testCreatXml.xml");
if( !file.exists()||!file.isFile()){
new FileOutputStream("d:/testCreatXml.xml");
file = new File("d:/testCreatXml.xml");
}
StreamResult streamResult = new StreamResult(file);
Source inputSource = new DOMSource(doc);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(inputSource, streamResult);


}
}

阅读(1274) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~