Example usage for javax.xml.transform Transformer transform

List of usage examples for javax.xml.transform Transformer transform

Introduction

In this page you can find the example usage for javax.xml.transform Transformer transform.

Prototype

public abstract void transform(Source xmlSource, Result outputTarget) throws TransformerException;

Source Link

Document

Transform the XML Source to a Result.

Usage

From source file:Main.java

/**
 * Transform.//from  w ww .j  a v a2 s .  c  o  m
 *
 * @param xmlSource
 *            the xml source
 * @param xsltSource
 *            the xslt source
 * @param outputStream
 *            the output stream
 * @throws TransformerFactoryConfigurationError
 *             the transformer factory configuration error
 * @throws TransformerConfigurationException
 *             the transformer configuration exception
 * @throws TransformerException
 *             the transformer exception
 */
public static void transform(Source xmlSource, Source xsltSource, OutputStream outputStream)
        throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException {
    Transformer transformer = getTransformer(xsltSource);

    transformer.transform(xmlSource, new StreamResult(outputStream));
}

From source file:Main.java

/**
 * Serialize W3C document into given output stream
 * @param doc W3C document/*from  w w  w .j  a  va  2  s.  c o m*/
 * @param out OutputStream 
 * @throws TransformerException
 */
public static void print(Document doc, OutputStream out) throws TransformerException {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();

    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(out);
    transformer.transform(source, result);
}

From source file:Main.java

/**
 * Converts a DOM Tree to a String.//  ww w  .ja  v a2  s  .  c o  m
 *
 * @param xml The DOM document to convert.
 *
 * @return A string containing the XML if successful, null otherwise.
 */
public static String xmlToString(Document xml) {
    try {
        TransformerFactory f = TransformerFactory.newInstance();
        StringWriter writer = new StringWriter();
        Transformer t = f.newTransformer();
        DOMSource src = new DOMSource(xml);
        StreamResult result = new StreamResult(writer);
        t.transform(src, result);
        return writer.toString();
    } catch (TransformerException e) {
        return null;
    }
}

From source file:Main.java

public static void prettyFormatXml(final InputStream xml, final OutputStream os, final int indent) {
    try {/*w  ww. j  a va  2s . c  o m*/
        final Source xmlInput = new StreamSource(xml);
        final StreamResult xmlOutput = new StreamResult(os);
        final TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", indent);
        final Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(xmlInput, xmlOutput);
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static void xmlToStreamE(Node n, OutputStream os) {
    try {// ww  w . j a v  a 2  s.  c  o m
        Source source = new DOMSource(n);

        //  PrintWriter pr = new PrintWriter(new OutputStreamWriter(os,StandardCharsets.UTF_8),true);
        Result result = new StreamResult(os);//pr);
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.transform(source, result);
    } catch (IllegalArgumentException | TransformerException e) {
        throw new Error(e);
    }
}

From source file:Main.java

public static void transformNonTextNodeToOutputStream(Node node, OutputStream os, boolean omitXmlDeclaration)
        throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException {

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer trans = tf.newTransformer();
    if (omitXmlDeclaration) {
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    }/*from  w  w w .  j a v a  2 s .com*/
    trans.transform(new DOMSource(node), new StreamResult(os));
}

From source file:Main.java

public static String nodeAsString(Node node) {
    String nodeStr = "";
    TransformerFactory tff = TransformerFactory.newInstance();
    try {//w w  w.  j a  va  2 s. c  om
        Transformer tf = tff.newTransformer();
        tf.setOutputProperty("encoding", "UTF-8");

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        tf.transform(new DOMSource(node), new StreamResult(bos));
        return bos.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return nodeStr;
}

From source file:Main.java

public static void toText(Node node, OutputStream out)
        throws TransformerFactoryConfigurationError, TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    Source source = new DOMSource(node);
    Result output = new StreamResult(out);
    transformer.transform(source, output);
}

From source file:Main.java

public static String formatDocument2(Document document) throws TransformerException {
    DOMSource domSource = new DOMSource(document);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    StreamResult streamResult = new StreamResult(out);
    TransformerFactory tf = TransformerFactory.newInstance();

    Transformer serializer = tf.newTransformer();
    serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.transform(domSource, streamResult);
    return new String(out.toByteArray());
}

From source file:Main.java

public static void transform(Document src, Writer dst, String xsl) throws TransformerException {

    DOMSource dsrc = new DOMSource(src);
    StreamResult res = new StreamResult(dst);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer(new StreamSource(xsl));
    t.setOutputProperty(OutputKeys.METHOD, "xml");
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    t.transform(dsrc, res);

}