Java XML Transform newTransformer(boolean indented)

Here you can find the source of newTransformer(boolean indented)

Description

new Transformer

License

Apache License

Declaration

public static Transformer newTransformer(boolean indented) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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

import javax.xml.transform.TransformerFactory;

public class Main {
    private static final Integer DEFAULT_INDENT = Integer.valueOf(4);

    public static Transformer newTransformer(boolean indented) {

        String indentFlag = (indented) ? "yes" : "no";

        try {// w ww  .j av a 2  s  .c o  m

            TransformerFactory factory = TransformerFactory.newInstance();
            factory.setAttribute("indent-number", DEFAULT_INDENT);

            Transformer transformer;
            transformer = factory.newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
            transformer.setOutputProperty(OutputKeys.INDENT, indentFlag);
            transformer.setOutputProperty(OutputKeys.METHOD, "xml");

            return transformer;

        } catch (TransformerConfigurationException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. getXsltTransformer(File xsltFile)
  2. internalTransformWithParams(Reader doc, Templates templates, Result r, boolean trace, String[] params)
  3. newTransformer()
  4. newTransformer()
  5. newTransformer()
  6. newTransformer(int indent)
  7. newTransformerFactory()
  8. newTransformerFactory()
  9. newTransformerFactory()