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 String formatDocument2(Document document) throws TransformerException {
    DOMSource domSource = new DOMSource(document);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    StreamResult streamResult = new StreamResult(out);
    TransformerFactory tf = TransformerFactory.newInstance();

    Transformer serializer = tf.newTransformer();
    serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.transform(domSource, streamResult);
    return new String(out.toByteArray());
}

From source file:Main.java

public static String toString(Document doc) throws TransformerException {
    StringWriter sw = new StringWriter();
    TransformerFactory tfactory = TransformerFactory.newInstance();
    Transformer transformer = tfactory.newTransformer();
    transformer.transform(new DOMSource(doc), 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 String elementToString(Element ele) {
    try {/* w ww . j  a v a  2s  .  c  o  m*/
        StringWriter sw = new StringWriter();

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

        DOMSource source = new DOMSource(ele);
        StreamResult result = new StreamResult(sw);
        transformer.setOutputProperty("indent", "yes");
        transformer.transform(source, result);

        return sw.toString();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static void print(Document doc) {
    try {//from  w  w w. j ava2s . c  o  m
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        transformer.setOutputProperty("encoding", "utf-8");

        transformer.transform(new DOMSource(doc),
                new StreamResult(new OutputStreamWriter(System.out, "utf-8")));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void writeXmlToStream(Document doc, OutputStream stream) throws Exception {
    Result result = new StreamResult(stream);
    transform(doc, result);//from w  w  w.jav a  2  s  .  c o m
}

From source file:Main.java

public static String convertDocumentToString(Document doc) throws IOException, 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

public static String encodeBase64(Element elm) throws Exception {
    ByteArrayOutputStream ostream = new ByteArrayOutputStream();
    TransformerFactory transFac = TransformerFactory.newInstance();
    Transformer trans = transFac.newTransformer();
    trans.transform(new DOMSource(elm), new StreamResult(ostream));
    return (new String(Base64.encodeBase64(ostream.toByteArray(), true)));
}

From source file:Main.java

public static void writeXmlToFile(Document doc, String filename) {
    File file = new File(filename);
    Result result = new StreamResult(file);
    writeXmlToStream(doc, result);/* w  w w  .j av a  2  s . c o m*/

}

From source file:Main.java

public static void writeXml(Document dom, OutputStream os) throws TransformerException {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(dom);
    StreamResult result = new StreamResult(os);
    transformer.transform(source, result);
}