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

private static void writeChangesToXml(Document doc, String xmlPath) {
    try {/*from   www. j  av  a 2 s . c  o m*/
        DOMSource source = new DOMSource(doc);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        StreamResult result = new StreamResult(xmlPath);
        transformer.transform(source, result);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static String getStringFromDocument(Document doc) {
    DOMSource domSource = new DOMSource(doc);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer;
    try {//w  w w. ja  v  a 2  s.  co m
        transformer = tf.newTransformer();
        transformer.transform(domSource, result);
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }

    return writer.toString();
}

From source file:Main.java

public static void print(Document doc) {
    try {/*  w  ww  . jav a2  s.c o m*/
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        transformer.setOutputProperty("encoding", "utf-8");

        transformer.transform(new DOMSource(doc),
                new StreamResult(new OutputStreamWriter(System.out, "utf-8")));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Write a XML document down to a file.//from w ww .  j  a  v a2  s  .c  om
 */
public static void writeXmlToFile(Document document, File file) throws TransformerException {
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.transform(new DOMSource(document), new StreamResult(file));
}

From source file:Main.java

/**
 * Renders an XML node to a string/*from   w ww .  j a va  2 s  .  c  om*/
 * @param node The xml node to render
 * @param stripHeader If true, strips the auto-generated XML header from the generated string
 * @return the rendered string or null if it failed conversion
 */
public static String renderNode(final Node node, final boolean stripHeader) {
    if (node == null)
        return null;
    try {
        StringWriter writer = new StringWriter();
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(new DOMSource(node), new StreamResult(writer));
        final String s = writer.toString();
        if (stripHeader) {
            return s.replace(XML_HEADER, "");
        }
        return s;
    } catch (Throwable e) {
        return null;
    }
}

From source file:Main.java

public static String xmlToString(Node node) {
    try {/*from  w w w. j a v  a2 s . c om*/
        Source source = new DOMSource(node);
        StringWriter stringWriter = new StringWriter();
        Result result = new StreamResult(stringWriter);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(source, result);
        return stringWriter.getBuffer().toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:StreamSrcStAXRst.java

public static void transform(InputStream xmlIn, InputStream xsltIn, OutputStream out) throws Exception {
    javax.xml.transform.Source xmlSource = new javax.xml.transform.stream.StreamSource(xmlIn);
    javax.xml.transform.Result xmlResult = new javax.xml.transform.stax.StAXResult(
            XMLOutputFactory.newInstance().createXMLStreamWriter(new FileWriter(fileName)));

    javax.xml.transform.Source xsltSource = new javax.xml.transform.stream.StreamSource(xsltIn);
    javax.xml.transform.Source source = new javax.xml.transform.stream.StreamSource(xmlIn);

    // create an instance of TransformerFactory
    javax.xml.transform.TransformerFactory transFact = javax.xml.transform.TransformerFactory.newInstance();
    transFact.setAttribute("debug", new String("true"));
    transFact.setAttribute("translet-name", new String("suresh"));
    transFact.setAttribute("generate-translet", new String("true"));
    transFact.setAttribute("jar-name", new String("transletjar"));

    javax.xml.transform.Transformer trans = transFact.newTransformer(xsltSource);

    trans.transform(source, xmlResult);
}

From source file:Main.java

/**
 * Returns a string representation of {@code node}. This method should only
 * be used for debugging purposes, at it creates a new transformer factory
 * and transformer upon each call.//from  w  w  w. j  av a 2s.co m
 * 
 * @param n
 *            the node
 * @return a string representation of {@code node}
 */
public static String toString(Node n) {
    try {
        Transformer t = TransformerFactory.newInstance().newTransformer();
        StringWriter sw = new StringWriter();
        t.transform(new DOMSource(n), new StreamResult(sw));
        return sw.toString();
    } catch (TransformerException e) {
        return null;
    }
}

From source file:Main.java

private static void save(Document document, OutputStream outputStream)
        throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException,
        IOException {/*from w  w  w  .j  a  v  a  2s.co m*/
    DOMSource source = new DOMSource(document);
    StreamResult result = new StreamResult(outputStream);
    TransformerFactory transFactory = TransformerFactory.newInstance();
    Transformer transformer = transFactory.newTransformer();
    transformer.setOutputProperty("indent", "yes");
    transformer.transform(source, result);
    outputStream.close();
}

From source file:Main.java

/** Write an XML DOM tree to a file
 *
 * @param doc the document to write//from  w w w  .j a  va 2 s  .c o  m
 * @param file the file to write to
 * @throws IOException if a file I/O error occurs
 */
public static void write(Document doc, File file) throws IOException {
    try {
        DOMSource domSource = new DOMSource(doc);
        FileOutputStream stream = new FileOutputStream(file);
        // OutputStreamWriter works around indent bug 6296446 in JDK
        // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6296446
        StreamResult streamResult = new StreamResult(new OutputStreamWriter(stream));
        TransformerFactory tf = TransformerFactory.newInstance();
        tf.setAttribute("indent-number", new Integer(2));
        Transformer serializer = tf.newTransformer();
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.transform(domSource, streamResult);
    } catch (TransformerException e) {
        // This exception is never thrown, treat as fatal if it is
        throw new RuntimeException(e);
    }
}