Java tutorial
//package com.java2s; /* * Copyright (c) 2013 VMware, Inc. All Rights Reserved. * * 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.FileOutputStream; import java.io.OutputStream; 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; public class Main { /** * Saves Document instance to XML file to the specified filePath. * * @param xmlDoc - Document instance * @param destPath - dest path where the XML file is to be saved. * @throws Exception */ public static void saveDocument(Document xmlDoc, String destPath) throws Exception { Source xmlSource = new DOMSource(xmlDoc); OutputStream os = new FileOutputStream(destPath); Result result = new StreamResult(os); TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); /* output to file */ transformer.transform(xmlSource, result); } }