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

/**
 * This method will apply an XSLT transformation
 * @param source the source reader//from w  ww.ja  va2 s.  c o  m
 * @param result the target writter
 * @param style the stylesheet to be applied
 * @throws TransformerException
 */
static void transform(Reader source, Writer result, String style) throws TransformerException {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer(new StreamSource(new StringReader(style)));
    transformer.transform(new StreamSource(source), new StreamResult(result));
}

From source file:Main.java

/**
 * Writes the given {@link Document} to an {@link OutputStream} using
 * UTF-8 encoding.//from  w  ww  .  j  av a 2  s . co  m
 *
 * @param doc The {@link Document} to write.
 * @param out The {@link OutputStream} to write to.
 * @see #encodeDocument(Document,boolean)
 * @see #writeDocumentTo(Document,File)
 */
public static void writeDocumentTo(Document doc, OutputStream out) throws IOException {
    final DOMSource source = new DOMSource(doc);
    final Writer writer = new OutputStreamWriter(out, "UTF-8");
    final StreamResult result = new StreamResult(writer);
    final Transformer xform = createTransformer();
    try {
        xform.transform(source, result);
    } catch (TransformerException e) {
        throw new IOException("Couldn't write XML: " + e.getMessage());
    }
}

From source file:Main.java

/** Transform a XML Document to String */
public static String toString(Document doc) {
    try {/*  w  w  w  . ja v a 2  s.com*/
        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();
    } catch (Exception e) {
        e.printStackTrace();
        return "";
    }
}

From source file:Main.java

public static String saveXmlToStreamWriter(Document doc) throws TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(writer));

    return writer.getBuffer().toString();
}

From source file:Main.java

/**
 * Persists the XML to the file system./*from   w ww  .  j a v  a  2 s. c  om*/
 *
 * @param src the source XML.
 * @param file the file to persist to.
 * @throws TransformerException
 */
public static void persist(Source src, File file) throws TransformerException {
    final TransformerFactory tranFactory = TransformerFactory.newInstance();
    final Transformer aTransformer = tranFactory.newTransformer();
    final Result dest = new StreamResult(file);
    aTransformer.transform(src, dest);
}

From source file:Main.java

/**
 * Renders an XML node to a string// w ww.  ja v a2 s.c om
 * @param node The xml node to render
 * @return the rendered string or null if it failed conversion
 */
public static String renderNode(Node node) {
    if (node == null)
        return null;
    try {
        StringWriter writer = new StringWriter();
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(new DOMSource(node), new StreamResult(writer));
        return writer.toString();
    } catch (Throwable e) {
        return null;
    }

}

From source file:Main.java

public static void createSVGFile(File file, Document document) throws TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    Result svgOutput = new StreamResult(file);
    transformer.transform(new DOMSource(document), svgOutput);
}

From source file:Main.java

public static String nodeToString(Node n, boolean isOmitXmlDeclaration, boolean isIndent)
        throws TransformerException {
    StringWriter sw = new StringWriter();
    Transformer transformer = generateTransformer(isOmitXmlDeclaration, isIndent);
    transformer.transform(new DOMSource(n), new StreamResult(sw));
    return sw.toString();
}

From source file:Main.java

public static void printXml(Document doc, Writer writer) throws Exception {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
}

From source file:Main.java

public static boolean write(Document doc, String path) {
    boolean result = false;

    try {//from   w ww.  j av a 2 s.c  om
        DOMSource inputDoc = new DOMSource(doc);
        StreamResult sr = new StreamResult(path);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.transform(inputDoc, sr);
        result = true;
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }

    return result;
}