Java XML Transform transform(javax.xml.transform.Source source, javax.xml.transform.Result result, boolean indent)

Here you can find the source of transform(javax.xml.transform.Source source, javax.xml.transform.Result result, boolean indent)

Description

Executes a transformation.

License

Apache License

Parameter

Parameter Description
source the transformation source
result the transformation result
indent if true, the output indent key is set to "yes"

Exception

Parameter Description
TransformerException if an exception occurs

Declaration

public static void transform(javax.xml.transform.Source source, javax.xml.transform.Result result,
        boolean indent) throws TransformerException 

Method Source Code

//package com.java2s;
/* See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * Esri Inc. 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./*w w  w .j  a v  a 2 s  .  c o  m*/
 */

import javax.xml.XMLConstants;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;

public class Main {
    /** DEFAULT_ENCODING = "UTF-8" */
    public static final String DEFAULT_ENCODING = "UTF-8";

    /**
     * Executes a transformation.
     * <br>The output encoding is set to UTF-8
     * @param source the transformation source
     * @param result the transformation result
     * @param indent if true, the output indent key is set to "yes"
     * @throws TransformerException if an exception occurs
     */
    public static void transform(javax.xml.transform.Source source, javax.xml.transform.Result result,
            boolean indent) throws TransformerException {
        TransformerFactory factory = TransformerFactory.newInstance();
        factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
        factory.setFeature("http://javax.xml.XMLConstants/feature/secure-processing", true);
        //factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl",true); 
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, DEFAULT_ENCODING);
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        if (indent) {
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        }
        transformer.transform(source, result);
    }
}

Related

  1. transform(InputStream styleSheet, InputStream xml, Writer out)
  2. transform(InputStream styleSheet, InputStream xml, Writer out)
  3. transform(InputStream xmlStream, InputStream xslStream, OutputStream outputStream)
  4. transform(InputStream xslis, InputStream xmlis, OutputStream xmlos)
  5. transform(InputStream xsltInputStream, InputStream xmlInputStream, OutputStream outputStream, Map parameterMap)
  6. transform(Reader source, Writer result, String style)
  7. transform(SOAPPart part)
  8. transform(Source source, Result res)
  9. transform(Source source, Result result)