Java XML Document Transform transform(final Document document, final StreamResult streamResult, final boolean xmlDeclaration)

Here you can find the source of transform(final Document document, final StreamResult streamResult, final boolean xmlDeclaration)

Description

Transform.

License

Open Source License

Parameter

Parameter Description
document the document
streamResult the stream result
xmlDeclaration the xml declaration

Declaration

public static void transform(final Document document, final StreamResult streamResult,
        final boolean xmlDeclaration) 

Method Source Code

//package com.java2s;
/***********************************************************************************************************************
 * Copyright (c) 2008 empolis GmbH and brox IT Solutions GmbH. All rights reserved. This program and the accompanying
 * materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors: Ivan Churkin (brox IT Solutions GmbH) - initial creator
 *               Sebastian Voigt (brox IT Solutions GmbH)
 *               Andreas Weber (empolis GmbH) - switch to XML 1.1 in header to make more documents readable.
 **********************************************************************************************************************/

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;

public class Main {
    /**/*from  ww w  .j  a  v  a 2 s  .  c  o m*/
     * The Constant TRANSFORMER_FACTORY.
     */
    public static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactory.newInstance();

    /**
     * Transform.
     *
     * @param document
     *          the document
     * @param streamResult
     *          the stream result
     * @param xmlDeclaration
     *          the xml declaration
     */
    public static void transform(final Document document, final StreamResult streamResult,
            final boolean xmlDeclaration) {
        if (document == null) {
            throw new IllegalArgumentException("document cannot be NULL!");
        }
        if (streamResult == null) {
            throw new IllegalArgumentException("streamResult cannot be NULL!");
        }
        Transformer transformer;
        try {
            transformer = TRANSFORMER_FACTORY.newTransformer();
        } catch (final TransformerConfigurationException e) {
            throw new RuntimeException(e);
        }
        transformer.setOutputProperty(OutputKeys.INDENT, "no");
        String strXmlDeclaration;
        if (xmlDeclaration) {
            strXmlDeclaration = "no";
        } else {
            strXmlDeclaration = "yes";
        }
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, strXmlDeclaration);
        final DOMSource source = new DOMSource(document);
        try {
            transformer.transform(source, streamResult);
        } catch (final TransformerException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. transform(Document doc, Result result)
  2. transform(Document doc, Result result)
  3. transform(Document doc, StreamSource xslInput, String systemid)
  4. transform(Document document, Properties outputProperties, StreamResult streamResult)
  5. transform(Document dom, Document xslt)
  6. transform(Source xsl, Document doc)
  7. transformDoc(Document doc)
  8. transformDocToFile(Document doc, File file)
  9. transformDocument(Document doc, String xsl, String targetFile)