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

private static boolean writeTo(Document doc, String fileName) throws Exception {
    boolean isOver = false;
    DOMSource doms = new DOMSource(doc);
    File f = new File(fileName);
    StreamResult sr = new StreamResult(f);
    try {/* ww w .jav  a 2  s .  c  om*/
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        Properties properties = t.getOutputProperties();
        properties.setProperty(OutputKeys.ENCODING, "UTF-8");
        t.setOutputProperties(properties);
        t.transform(doms, sr);
        isOver = true;
    } catch (TransformerConfigurationException tce) {
        tce.printStackTrace();
    } catch (TransformerException te) {
        te.printStackTrace();
    }
    return isOver;
}

From source file:Main.java

/**
 * Saves a given XML document to the given output stream.
 *///  w w w  .  j  a v  a  2 s.c o  m
public static void writeXML(Document document, OutputStream os) throws IOException {
    DOMSource src = new DOMSource(document);
    StreamResult res = new StreamResult(os);
    TransformerFactory tf = TransformerFactory.newInstance();
    try {
        Transformer t = tf.newTransformer();
        t.transform(src, res);
    } catch (TransformerException e) {
        throw new IOException(e.getMessage());
    }
}

From source file:Main.java

public static void transfor(Document doc, OutputStream out) {
    try {//from   www  . j a  va 2  s . c o  m
        Transformer tf = tff.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(out);
        tf.transform(source, result);
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static void saveReportToFile(Node node, String documentFileName) {
    // StringWriter writer = new StringWriter();
    // StreamResult resultString = new StreamResult(writer);

    File output = new File(documentFileName);
    StreamResult resultFile = new StreamResult(output);

    DOMSource source = new DOMSource(node);
    try {/* w w w. j  a v  a  2 s . c o  m*/
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.setOutputProperty(OutputKeys.METHOD, "xml");
        t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        t.transform(source, resultFile);
        // t.transform(source, resultString);
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    // return writer.toString();
}

From source file:Main.java

public static String convertDocumentToString(Document dom) {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer;//from w w  w  .j  a  v  a2 s.co  m
    try {
        transformer = tf.newTransformer();
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(dom), new StreamResult(writer));
        String output = writer.getBuffer().toString();
        return output;
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

static public String document2String(Node document) {
    if (document == null)
        return null;

    StringWriter stringWriter = new StringWriter();
    try {/*from w  w w . j a v a2 s  . c  o m*/
        identityTransformer.transform(new DOMSource(document), new StreamResult(stringWriter));
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return stringWriter.toString();
}

From source file:Main.java

/**
 * //from   w  ww.  j  a  v a 2s . c  o m
 * @param xml
 * @param indent
 * @return pretty formatted xml
 */
public static String prettyFormat(String xml, int indent) {
    try {
        Source xmlInput = new StreamSource(new StringReader(xml));
        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) {
        throw new RuntimeException(e); // simple exception handling, please review it
    }
}

From source file:Main.java

public static String node2String(Node node) throws Exception {
    if (node == null)
        return null;
    StringWriter out = null;//  w  ww. ja  v a2s .  c  o m

    try {
        Transformer transformer = transformerFactory.newTransformer();
        out = new StringWriter();
        transformer.transform(new DOMSource(node), new StreamResult(out));
        return out.toString();
    } catch (Exception e) {
        throw e;
    } finally {
        if (out != null)
            out.close();
    }
}

From source file:Main.java

public static String xml(TransformerFactory transformerFactory, Node node) {
    Writer writer = new StringWriter();
    DOMSource source = new DOMSource(node);
    StreamResult result = new StreamResult(writer);
    try {/*ww w .  j  a v  a 2s  .c  o  m*/
        transformerFactory.newTransformer().transform(source, result);
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
    return writer.toString().replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "");
}

From source file:Main.java

public static void toWriter(Document doc, Writer writer) {
    if (doc == null || writer == null) {
        return;//from w w w. j  a v a2  s  . c om
    }
    try {
        Transformer tran = tf.newTransformer();
        tran.setOutputProperty(OutputKeys.INDENT, "yes");
        tran.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        Source src = new DOMSource(doc);
        Result res = new StreamResult(writer);
        tran.transform(src, res);
    } catch (Exception e) {
        e.printStackTrace();
    }
}