Java tutorial
//package com.java2s; /* * Autopsy Forensic Browser * * Copyright 2012-2014 Basis Technology Corp. * Contact: carrier <at> sleuthkit <dot> org * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import javax.xml.transform.OutputKeys; import javax.xml.transform.Result; 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; public class Main { /** * Saves a WC3 DOM by writing it to an XML document. * * @param doc The WC3 DOM document object. * @param docPath The full path to the XML document. * @param encoding Encoding scheme to use for the XML document, e.g., * "UTF-8." * @throws TransformerConfigurationException * @throws FileNotFoundException * @throws UnsupportedEncodingException * @throws TransformerException * @throws IOException */ public static void saveDocument(final Document doc, String encoding, String docPath) throws TransformerConfigurationException, FileNotFoundException, UnsupportedEncodingException, TransformerException, IOException { TransformerFactory xf = TransformerFactory.newInstance(); xf.setAttribute("indent-number", 1); //NON-NLS Transformer xformer = xf.newTransformer(); xformer.setOutputProperty(OutputKeys.METHOD, "xml"); //NON-NLS xformer.setOutputProperty(OutputKeys.INDENT, "yes"); //NON-NLS xformer.setOutputProperty(OutputKeys.ENCODING, encoding); xformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); //NON-NLS xformer.setOutputProperty(OutputKeys.VERSION, "1.0"); File file = new File(docPath); try (FileOutputStream stream = new FileOutputStream(file)) { Result out = new StreamResult(new OutputStreamWriter(stream, encoding)); xformer.transform(new DOMSource(doc), out); stream.flush(); } } }