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

/**
 * To output a Node as a String./*from w ww.j  av a 2s  .com*/
 */
public static String nodeToString(Node document) throws Exception {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    Source xmlSource = new DOMSource(document);
    Result outputTarget = new StreamResult(outputStream);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.transform(xmlSource, outputTarget);

    return outputStream.toString();
}

From source file:Main.java

private static void writeTreeToXMLFile() {

    try {//from ww  w.ja va 2s . c om
        Source source = new DOMSource(dom);

        File file = new File("resources/Styles.xml");
        Result result = new StreamResult(file);

        Transformer xformer = TransformerFactory.newInstance().newTransformer();

        xformer.transform(source, result);
        System.out.println("End of write method");

    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:Main.java

/**
 * Write a XML document to file/*from  ww w .j av  a2  s  .  c o m*/
 * @param doc document to write
 * @param filename name of file to create
 */
public static void writeXmlFile(Document doc, String filename) {
    try {
        // Prepare the DOM document for writing
        Source source = new DOMSource(doc);

        // Prepare the output file
        File file = new File(filename);
        Result result = new StreamResult(file);

        // Write the DOM document to the file
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.transform(source, result);
    } catch (TransformerConfigurationException e) {
    } catch (TransformerException e) {
    }
}

From source file:Main.java

public static String documentToXmlString(Document document) {
    DOMSource domSource = new DOMSource(document);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer;
    try {/*from w  w  w  . j  av a  2  s. co  m*/
        transformer = tf.newTransformer();
        transformer.transform(domSource, result);
    } catch (TransformerException e) {
        throw new RuntimeException(e.getMessage());
    }
    return writer.toString();
}

From source file:Main.java

/**
 * Represent xml node as string//  w w  w .  j  av a 2  s .  com
 * @param node to be represented
 * @return node text
 */
public static String xmlToString(Node node) {
    try {
        Source source = new DOMSource(node);
        StringWriter stringWriter = new StringWriter();
        Result result = new StreamResult(stringWriter);
        Transformer transformer = getTransformerFactory().newTransformer();
        transformer.transform(source, result);
        return stringWriter.getBuffer().toString();
    } catch (TransformerException e) {
        throw new RuntimeException("Can't transfor XML to String", e);
    }
}

From source file:Main.java

public static void transform(Source xsltSource, Source xmlSource, Result outputTarget)
        throws TransformerException {
    Transformer t = transformer(xsltSource);
    try {//from   w ww.  j a  v a 2 s  . co m
        t.transform(xmlSource, outputTarget);
    } finally {
        t.reset();
    }
}

From source file:Main.java

public static String xmlToString(Document doc) throws Exception {

    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(doc.getDocumentElement()), new StreamResult(writer));
    String result = writer.toString();
    System.out.println(result);/*from ww w .j a v  a  2  s . c  o m*/
    return result;
}

From source file:Main.java

/**
 * @param xmlsource/*w  w w. j a v a2 s .c  o m*/
 * @param xsltfile
 * @return
 * @throws javax.xml.transform.TransformerException
 */
public static String transform(String xmlsource, File xsltfile) throws TransformerException {

    Source xmlSource = new StreamSource(new StringReader(xmlsource));
    Source xsltSource = new StreamSource(xsltfile);
    StringWriter stringWriter = new StringWriter();

    TransformerFactory transFact = TransformerFactory.newInstance();
    Templates cachedXSLT = transFact.newTemplates(xsltSource);
    Transformer trans = cachedXSLT.newTransformer();
    trans.transform(xmlSource, new StreamResult(stringWriter));

    return stringWriter.toString();
}

From source file:Main.java

/**
 * @param xmlsource//from   w  ww. j  a va2 s  . c om
 * @param xsltstream
 * @return
 * @throws javax.xml.transform.TransformerException
 */
public static String transform(String xmlsource, InputStream xsltstream) throws TransformerException {

    Source xmlSource = new StreamSource(new StringReader(xmlsource));
    Source xsltSource = new StreamSource(xsltstream);
    StringWriter stringWriter = new StringWriter();

    TransformerFactory transFact = TransformerFactory.newInstance();
    Templates cachedXSLT = transFact.newTemplates(xsltSource);
    Transformer trans = cachedXSLT.newTransformer();
    trans.transform(xmlSource, new StreamResult(stringWriter));

    return stringWriter.toString();
}

From source file:Main.java

public static void writeXmlFile(Document doc, String filename) {
    try {/*w  w  w. j  a  v a2s  . c  o  m*/
        // Prepare the DOM document for writing
        Source source = new DOMSource(doc);

        // Prepare the output file
        File file = new File(filename);
        Result result = new StreamResult(file);

        // Write the DOM document to the file
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.transform(source, result);
    } catch (TransformerConfigurationException ex) {
        System.out.println(ex.getMessage());
    } catch (TransformerException ex) {
        System.out.println(ex.getMessage());
    }
}