Java XML Transform writeByTransformer(Node node, Writer writer, int indent)

Here you can find the source of writeByTransformer(Node node, Writer writer, int indent)

Description

write By Transformer

License

Open Source License

Declaration

private static void writeByTransformer(Node node, Writer writer, int indent) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import org.w3c.dom.Document;

import org.w3c.dom.Node;

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

import java.io.IOException;

import java.io.Writer;

public class Main {
    private static void writeByTransformer(Node node, Writer writer, int indent) throws IOException {
        if (node.getNodeType() == Node.DOCUMENT_NODE) {
            ((Document) node).setXmlStandalone(true);
        }//  w  w w.  ja  v a2  s  .c  o  m
        try {
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, indent == 0 ? "no" : "yes");
            transformer.setOutputProperty(OutputKeys.METHOD, "xml");
            transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
            if (indent > 0) {
                transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount",
                        Integer.toString(indent));
            }
            transformer.setOutputProperty("{http://xml.apache.org/xalan}line-separator", "\n");
            transformer.transform(new DOMSource(node), new StreamResult(writer));
        } catch (TransformerConfigurationException e) {
            throw new IllegalStateException(e);
        } catch (TransformerException e) {
            throw new IllegalStateException(e);
        }
    }
}

Related

  1. transformViaXslt(String xml, String xslFile)
  2. transformXml(final StreamSource xslSrc, final StreamSource docSrc, final Map params, final URIResolver resolver)
  3. transformXML(Reader xml, Reader xsl)
  4. transformXmlByXslt(StreamSource source, URI xslFile)
  5. write_plain_content_tag(String name, String content, AttributesImpl atts, TransformerHandler hd)