Here you can find the source of getEmptyXmlFile(File xmlFile)
Parameter | Description |
---|---|
xmlFile | the file to write to |
Parameter | Description |
---|---|
Exception | an exception |
public static void getEmptyXmlFile(File xmlFile) throws Exception
//package com.java2s; import java.io.File; import java.io.FileOutputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.Result; 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 Main { /**/*from w w w . j a va 2 s.c o m*/ * creates a new xmlfile with a "reports" son for the reports usage * * @param xmlFile * the file to write to * @throws Exception */ public static void getEmptyXmlFile(File xmlFile) throws Exception { Element main; DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = db.newDocument(); main = doc.createElement("reports"); doc.appendChild(main); saveDocumentToFile(doc, xmlFile); } public static void saveDocumentToFile(Document doc, File file) throws Exception { if (file.getParent() != null) { mkdirs(file.getParent()); } FileOutputStream fos = new FileOutputStream(file); Source source = new DOMSource(doc); Result result = new StreamResult(fos); Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); xformer.transform(source, result); fos.close(); } public static void mkdirs(String pathName) { File file = new File(pathName); file.mkdirs(); } }