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

License:asdf

public static String documentToString(Document document) {
    try {/* w w w.  j a  v a2 s  .  c om*/
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer trans = tf.newTransformer();
        StringWriter sw = new StringWriter();
        trans.transform(new DOMSource(document), new StreamResult(sw));
        return sw.toString();
    } catch (TransformerException tEx) {
        tEx.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String documentToString(Document document) throws TransformerException {
    Node rootNode = (Node) document.getDocumentElement();
    Source source = new DOMSource(rootNode);

    StringWriter stringWriter = new StringWriter();
    Result result = new StreamResult(stringWriter);
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    transformer.transform(source, result);

    return stringWriter.getBuffer().toString().replace("\n", "");
}

From source file:Main.java

private static void doTransform(Source xslSource, Source xmlSource, Result xslResult) {
    try {/*  www. ja  v a  2  s. co  m*/
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer(xslSource);
        transformer.transform(xmlSource, xslResult);
    } catch (TransformerFactoryConfigurationError ex) {
        throw new RuntimeException(ex);
    } catch (TransformerException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:Main.java

private static void transfer(Document doc, Result streamResult)
        throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException {

    Source source = new DOMSource(doc);
    TransformerFactory xformFactory = TransformerFactory.newInstance();
    Transformer idTransform = xformFactory.newTransformer();
    idTransform.transform(source, streamResult);
}

From source file:Main.java

/**
 * Creates a {@link String} out of a {@link Node}
 * /*from w ww .j a  v a 2 s .c  o  m*/
 * @param node
 *            never <code>null</code>
 * @return the node as string, never <code>null</code>
 * @throws Exception
 */
public static String asString(Node node) throws Exception {
    StringWriter writer = new StringWriter();
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.transform(new DOMSource(node), new StreamResult(writer));
    return writer.toString();
}

From source file:Main.java

public static void transform(InputStream xslis, InputStream xmlis, OutputStream xmlos) {
    try {/*from  w w  w.j a v  a2  s .  c om*/
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(new StreamSource(xslis));
        transformer.transform(new StreamSource(xmlis), new StreamResult(xmlos));
        xslis.close();
        xmlis.close();
        xmlos.close();
    } catch (Exception e) {
        throw new RuntimeException("Fail to do XSLT transformation", e);
    }
}

From source file:Main.java

/**
 * Transforms a DOM node to a String./*w ww  .j  a v a  2s . c  om*/
 *
 * @param node the node
 * @return the transformed XML representation
 * @throws TransformerException if a transformation error occurs.
 */
public static String transform(Node node) throws TransformerException {
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    Source source = new DOMSource(node);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.transform(source, result);
    return writer.toString();
}

From source file:Main.java

/**
 * Convert XML Document to a String.// www. j  a va2 s  . c  o  m
 * 
 * @param node
 * @return
 */
public static String xmlToString(Node node) {
    try {
        Source source = new DOMSource(node);
        StringWriter stringWriter = new StringWriter();
        Result result = new StreamResult(stringWriter);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.transform(source, result);
        return stringWriter.getBuffer().toString();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static void printDom(Document dom, String filename) {

    try {/*ww w.  j av  a  2  s .c  o  m*/
        DOMSource source = new DOMSource(dom);
        StreamResult result;
        result = new StreamResult(new FileOutputStream(filename));

        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer();

        transformer.transform(source, result);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

/**
 * convert org.w3c.dom.Document to xml-String
 * //www.  j a  v a  2  s  . c o  m
 * @param document
 * @return String xml
 * @throws Exception
 */
private static String toString(Document document) throws Exception {
    Source source = new DOMSource(document);
    StringWriter stringWriter = new StringWriter();
    Result result = new StreamResult(stringWriter);
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    transformer.transform(source, result);
    return stringWriter.getBuffer().toString();
}