Example usage for javax.xml.transform.stream StreamResult StreamResult

List of usage examples for javax.xml.transform.stream StreamResult StreamResult

Introduction

In this page you can find the example usage for javax.xml.transform.stream StreamResult StreamResult.

Prototype

public StreamResult(File f) 

Source Link

Document

Construct a StreamResult from a File.

Usage

From source file:Main.java

public static void ElementToStream(Element element, OutputStream out) {
    try {//  www.  j a  v  a 2  s  .c o m
        DOMSource source = new DOMSource(element);
        StreamResult result = new StreamResult(out);
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer();
        transformer.transform(source, result);
    } catch (Exception ex) {
    }
}

From source file:Main.java

public static String transformDOMToString(DOMSource source) {
    try {/*www. j  a  va2  s. co m*/
        // Use a Transformer for output
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        ByteArrayOutputStream sos = new ByteArrayOutputStream();
        StreamResult result = new StreamResult(sos);
        transformer.transform(source, result);
        return sos.toString();
    } catch (TransformerException e) {
        throw new IllegalArgumentException(e);
    }

}

From source file:Main.java

public static String transText(Node doc) throws Exception {
    TransformerFactory transfactory = TransformerFactory.newInstance();
    Transformer transformer = transfactory.newTransformer();
    transformer.setOutputProperty("omit-xml-declaration", "yes");
    transformer.setOutputProperty("indent", "yes");

    Source source = new DOMSource(doc);
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    transformer.transform(source, result);
    return sw.toString();
}

From source file:Main.java

public static String documentToString(Document document) {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {/*from   w ww. j  a va  2s.  c o  m*/
        transformer = tf.newTransformer();
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException(e);
    }
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    try {
        transformer.transform(new DOMSource(document), new StreamResult(writer));
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
    String output = writer.getBuffer().toString().replaceAll("\n|\r", "");
    return output;
}

From source file:Main.java

/**
 * Formats a xml string//w  w  w  .j a v  a2s.c  o m
 * @param input
 * @param indent
 * @return
 */
public static String prettyFormatXml(String input, int indent) {
    try {
        Source xmlInput = new StreamSource(new StringReader(input));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", indent);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(xmlInput, xmlOutput);

        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return input;
}

From source file:Main.java

/**
 * Write a XML document down to a file.//from   w  w w. j  av  a2  s .  c  o  m
 */
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

public static OutputStream writeDocument(Document doc, OutputStream os) {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer;/*  w w w .  ja v  a2  s . c  o m*/
    try {
        transformer = tFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(os);
        transformer.transform(source, result);
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return os;
}

From source file:Main.java

public static int node2Stream(Node node, OutputStream out) throws Exception {
    if (node == null || out == null)
        return -1;

    try {//  w w w. j ava  2  s. c  om
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        transformer.transform(new DOMSource(node), new StreamResult(out));
        return 0;
    } catch (Exception e) {
        throw e;
    }
}

From source file:Main.java

public static String nodeToString(Node node) {
    StringWriter sw = new StringWriter();
    try {/*  w w  w .j  a va2 s. c  o  m*/
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.transform(new DOMSource(node), new StreamResult(sw));
    } catch (TransformerException te) {
        throw new RuntimeException(te);
    }
    return sw.toString();
}

From source file:Main.java

public static String transform2String(Node node) throws TransformerException {
    final TransformerFactory transFactory = TransformerFactory.newInstance();
    final Transformer transformer = transFactory.newTransformer();
    final StringWriter buffer = new StringWriter();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.transform(new DOMSource(node), new StreamResult(buffer));
    return buffer.toString();
}