package test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class JavaFileDomo{
//e 生成文件路径
private static String path = "D:\\file\\";
public static void main(String argv[])
{
String time=new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
String fileName = path + "test2222"+time+".xml";
String filecontent = "<teacher><name>张三</name><workage>2</workage></teacher>";
//e 先删除原有文件
Boolean delBool = delFile(fileName);
//e 创建新的文件
Document doc = str2Document(filecontent);
createXml(fileName,doc);
}
public static Document str2Document(String xmlStr) {
Document doc = null;
StringReader sr = new StringReader(xmlStr);
InputSource is = new InputSource(sr);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
doc = builder.parse(is);
} catch (ParserConfigurationException e) {
System.out.println("ParserConfiguration错误"+e);
} catch (SAXException e) {
System.out.println("SAX错误"+e);
} catch (IOException e) {
System.out.println("IO错误"+e);
}
return doc;
}
public synchronized static void createXml(String filenameTemp, Document document) {
try {
//生成xml文件
File file = new File(filenameTemp);
//判断是否存在,如果不存在,则创建
if(!file.exists()){
file.createNewFile();
}
/** 将document中的内容写入文件中 */
TransformerFactory tFactory = TransformerFactory.newInstance();
tFactory.setAttribute("indent-number", new Integer(2));
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "");
// transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new FileOutputStream(filenameTemp));
transformer.transform(source, result);
System.out.println("--------------------------------" + "更新 XML文件成功" + "-------------------------------------");
} catch (final Exception exception) {
System.out.println("更新" + filenameTemp + "出错:"+exception);
}
}
/**
* 删除文件
* @param fileName 文件名称
* @return
*/
public static boolean delFile(String filenameTemp){
Boolean bool = false;
File file = new File(filenameTemp);
try {
if(file.exists()){
file.delete();
bool = true;
}
} catch (Exception e) {
// TODO: handle exception
}
return bool;
}
}
缺点:xml文件无法美化,没有缩行!!!!请看下篇为文档 使用dom4j生成xml,建议使用dom4j!!!