Java tutorial
//package com.java2s; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.OutputStream; 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 { static String baseDir = "dist" + File.separator + "signatures"; /** * Store xml Signature represented by dom.Document into xml file * @param doc - XML Signature * @param fileName - name of output file */ public static boolean storeSignatureToXMLFile(Document doc, String fileName) { boolean stored = false; OutputStream os; File absolute = new File(fileName); String outputPath = new String(); if (absolute.getParentFile().isAbsolute()) { outputPath = fileName; //path is absolute } else { outputPath = baseDir + File.separator + fileName; //relative, store it into %project_dir%/dist/signatures } File file = new File(outputPath); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } try { os = new FileOutputStream(outputPath); TransformerFactory tf = TransformerFactory.newInstance(); Transformer trans = tf.newTransformer(); //trans.setOutputProperty(OutputKeys.ENCODING, "utf-8"); //trans.setOutputProperty(OutputKeys.INDENT, "no"); //trans.setOutputProperty(OutputKeys.METHOD, "xml"); trans.transform(new DOMSource(doc), new StreamResult(os)); stored = true; } catch (FileNotFoundException e) { handleError(e); } catch (TransformerConfigurationException e) { handleError(e); } catch (TransformerException e) { handleError(e); } return stored; } private static final void handleError(Throwable ex) { ex.printStackTrace(); // ... handle error here... } }