Java tutorial
//package com.java2s; import java.io.File; 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.TransformerConfigurationException; import javax.xml.transform.TransformerException; 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 { private static final String ID_STORAGE_LOCATION = System.getProperty("user.home") + File.separator + "idHistory.xml"; private static final String IDS = "ids"; public static synchronized void createIdStorage() throws ParserConfigurationException, TransformerException { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.newDocument(); Element rootElement = doc.createElement(IDS); doc.appendChild(rootElement); Transformer transformer = getTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File(ID_STORAGE_LOCATION)); transformer.transform(source, result); } private static Transformer getTransformer() throws TransformerConfigurationException { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); // Formatting XML properly transformer.setOutputProperty(OutputKeys.INDENT, "yes"); return transformer; } }