Java tutorial
//package com.java2s; /* * Copyright (C) 2006-2015 Ronald Blankendaal * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; public class Main { public static void saveDomSource(final DOMSource source, final File target, final File xslt) throws TransformerException, IOException { TransformerFactory transFact = TransformerFactory.newInstance(); transFact.setAttribute("indent-number", 2); Transformer trans; if (xslt == null) { trans = transFact.newTransformer(); } else { trans = transFact.newTransformer(new StreamSource(xslt)); } trans.setOutputProperty(OutputKeys.INDENT, "yes"); FileOutputStream fos = new FileOutputStream(target); trans.transform(source, new StreamResult(new OutputStreamWriter(fos, "UTF-8"))); fos.close(); } }