Java XML Transform newTransformerHandler()

Here you can find the source of newTransformerHandler()

Description

Return a TransformerHandler.

License

Open Source License

Exception

Parameter Description
TransformerConfigurationException an exception

Return

a TransformerHandler

Declaration

public static TransformerHandler newTransformerHandler() throws TransformerConfigurationException 

Method Source Code

//package com.java2s;
/*//from ww  w . ja  va  2s.  c om
 * utils-java8
 * Copyright (C) 2014 Raffaele Ragni
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;

import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;

public class Main {
    private static final String UTF8 = "UTF-8";
    private static final String YES = "yes";
    private static SAXTransformerFactory tf = null;

    /**
     * Return a TransformerHandler.
     *
     * This is the instance object to be kept as reference for building an XML
     * document.
     *
     * - Encoding: UTF-8 - Indent: Yes - Omit XML declaration: Yes
     *
     * @return a TransformerHandler
     *
     * @throws TransformerConfigurationException
     */
    public static TransformerHandler newTransformerHandler() throws TransformerConfigurationException {
        TransformerHandler hd = tf.newTransformerHandler();
        Transformer serializer = hd.getTransformer();
        serializer.setOutputProperty(OutputKeys.ENCODING, UTF8);
        serializer.setOutputProperty(OutputKeys.INDENT, YES);
        serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, YES);
        return hd;
    }
}

Related

  1. newTransformer(boolean indented)
  2. newTransformer(int indent)
  3. newTransformerFactory()
  4. newTransformerFactory()
  5. newTransformerFactory()
  6. newTransformerHandler()
  7. newTransformerHandler(final SAXTransformerFactory tf)
  8. serializeXML(Transformer transformer, Source input)
  9. transform(byte[] doc, URL xsltUrl, OutputStream out)