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

public static void transform(Source xmlSource, Result outputTarget) throws TransformerException {
    Transformer t = transformer();
    try {/*from w  ww  . j  av a2 s  .  c o m*/
        t.transform(xmlSource, outputTarget);
    } finally {
        t.reset();
    }
}

From source file:Main.java

/**
 * Converts an XML document or node to an equivalent string. Applies the
 * reverse operation of {@link #xmlStringToDocument(String)}.
 *
 * @param d the node to convert//from   w  w  w .j  a  va 2s .  c o  m
 * @return a String representation of the XML node
 * @throws TransformerFactoryConfigurationError if an error occurs
 * configuring the transformer factory
 * @throws TransformerException if an error occurs during the transformation
 */
static String xmlDocumentToString(Node d) throws TransformerFactoryConfigurationError, TransformerException {
    StringWriter writer = new StringWriter();
    Transformer t = TransformerFactory.newInstance().newTransformer();
    t.transform(new DOMSource(d), new StreamResult(writer));
    return writer.toString();
}

From source file:Main.java

public static String getXMLString(Node doc) throws TransformerException {
    DOMSource domSource = new DOMSource(doc);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.transform(domSource, result);
    return writer.toString();
}

From source file:Main.java

/**
 * Perform an xsl transformation/*from w  w  w  .  j  ava 2s  . c o m*/
 */
public static void transform(Source xsl, Source xml, Result result) throws Exception {
    TransformerFactory factory = TransformerFactory.newInstance();
    Templates template = factory.newTemplates(xsl);
    Transformer transformer = template.newTransformer();
    transformer.transform(xml, result);
}

From source file:Main.java

/**
 * Writes an XML element to a given file
 * @param doc XML element//  w  ww  .  j  a v a  2s  .co  m
 * @param filename Filename of the file where to write XML
 */
public static void writeXmlFile(Element doc, String filename) {
    try {

        Source source = new DOMSource(doc);
        File file = new File(filename);
        Result result = new StreamResult(file);

        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.transform(source, result);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Returns String representation of the the XML node (document or element).
 * @param node the node//from   www. j  a  va  2 s.c om
 * @return string representation of the input
 * @throws Exception if an error occurs
 */
public static String toXml(Node node) throws Exception {
    Source source = new DOMSource(node);
    StringWriter writer = new StringWriter();
    Result result = new StreamResult(writer);
    Transformer t = TransformerFactory.newInstance().newTransformer();
    t.transform(source, result);
    return writer.toString();
}

From source file:Main.java

/**
 * Save content of DOM to a file.//from w ww  . j  av  a  2  s.  c om
 * @param doc Document
 * @param filePath String
 * @throws Exception
 */
public static void saveDomToFile(Document doc, String filePath) throws Exception {
    // Write the DOM to file.
    Source source = new DOMSource(doc);

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

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

From source file:Main.java

public static String transformString(String xmlString, String xsltFilePath) throws Exception {
    StreamResult result = new StreamResult(new StringWriter());
    StreamSource s = new StreamSource(new File(xsltFilePath));
    StreamSource xml = new StreamSource(new StringReader(xmlString));

    Transformer transformer = TransformerFactory.newInstance().newTransformer(s);
    transformer.transform(xml, result);

    String response = result.getWriter().toString();

    return response;
}

From source file:Main.java

public static void print(Node dom) {
    try {/*from  ww  w .  j a v a2  s  .  c o m*/
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer t = factory.newTransformer();
        StringWriter sw = new StringWriter();
        t.transform(new DOMSource(dom), new StreamResult(sw));
        System.out.println(sw.toString());
    } catch (Exception ex) {
        System.out.println("Could not print XML document");
    }
}

From source file:Main.java

public static void writeXMLFile(Node node, String filename) throws IOException {

    try {//from  w  w w  .j a v  a  2 s  .co m
        // Prepare the DOM document for writing
        DOMSource source = new DOMSource(node);
        // Prepare the output file

        StreamResult result = new StreamResult(filename);

        // Write the DOM document to the file
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.transform(source, result);

    } catch (TransformerConfigurationException e) {
        throw new IOException();
    } catch (TransformerException e) {
        throw new IOException();
    }
}