Here you can find the source of getSerializer(final File file)
public static ContentHandler getSerializer(final File file) throws TransformerException, IOException
//package com.java2s; /*//from ww w .j a v a2s.c om * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you 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.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.Properties; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.sax.TransformerHandler; import javax.xml.transform.stream.StreamResult; import org.xml.sax.ContentHandler; public class Main { /** The transformer factory. */ private static final SAXTransformerFactory FACTORY = (SAXTransformerFactory) TransformerFactory.newInstance(); /** * Get a serializer to write XML to a file. */ public static ContentHandler getSerializer(final File file) throws TransformerException, IOException { final Writer writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); final TransformerHandler transformerHandler = FACTORY.newTransformerHandler(); final Transformer transformer = transformerHandler.getTransformer(); final Properties format = new Properties(); format.put(OutputKeys.METHOD, "xml"); format.put(OutputKeys.OMIT_XML_DECLARATION, "no"); format.put(OutputKeys.ENCODING, "UTF-8"); format.put(OutputKeys.INDENT, "yes"); transformer.setOutputProperties(format); transformerHandler.setResult(new StreamResult(writer)); return transformerHandler; } }