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 toXmlString(Document doc) throws Exception {
    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);
    writer.flush();/* w  ww  . j  a  v a  2  s .  c om*/

    return writer.toString();
}

From source file:Main.java

public static synchronized void write2Xml(Document document) throws Exception {
    TransformerFactory.newInstance().newTransformer().transform(new DOMSource(document),
            new StreamResult(new FileOutputStream(filepath)));
}

From source file:Main.java

public static String prettyFormat(String input, int indent, boolean isOmitXmlDeclaration) {
    try {//from  ww  w  .  j  av  a  2 s  . c  om
        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");
        if (isOmitXmlDeclaration) {
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "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 void write2Xml(Document document) throws Exception {

    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer tf = factory.newTransformer();
    tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream(filename)));
}

From source file:Main.java

/**
 * //  w w w .ja v  a2 s.  c o m
 * @param doc
 * @return
 */
public static String document2String(Document doc) {
    try {
        DOMSource domSource = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        // transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        transformer.transform(domSource, result);
        return writer.toString();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static File saveToFile(String filename, Document document) throws TransformerException {
    // Prepare the DOM document for writing
    Source source = new DOMSource(document);

    // Prepare the output file
    File file = new File(filename);
    Result result = new StreamResult(file);

    // Write the DOM document to the file
    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    xformer.setOutputProperty(OutputKeys.INDENT, "yes");
    xformer.transform(source, result);// w w w.j  av a2  s .c  o  m

    return file;
}

From source file:Main.java

public static String nodeToString(Node node) throws TransformerException {
    StringWriter writer = new StringWriter();
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.transform(new DOMSource(node), new StreamResult(writer));
    return writer.toString().replaceAll("\\<\\?xml(.+?)\\?\\>", "").trim();
}

From source file:Main.java

public static String xml2Str(Document document) {

    try {/*  w  w w. ja  va2s.c  om*/
        DOMSource source = new DOMSource(document);
        StringWriter writer = new StringWriter();
        Result result = new StreamResult(writer);
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        transformer.transform(source, result);
        return (writer.getBuffer().toString());

    } catch (Exception e) {
        e.printStackTrace();
    }

    return "";
}

From source file:Main.java

private static void writeChangesToXml(Document doc, String xmlPath) {
    try {//  ww w  .j  av a  2  s. c  om
        DOMSource source = new DOMSource(doc);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        StreamResult result = new StreamResult(xmlPath);
        transformer.transform(source, result);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void transform(Document src, Writer dst, String xsl) throws TransformerException {

    DOMSource dsrc = new DOMSource(src);
    StreamResult res = new StreamResult(dst);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer(new StreamSource(xsl));
    t.setOutputProperty(OutputKeys.METHOD, "xml");
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    t.transform(dsrc, res);//from   w w  w.ja  v  a2s  .co  m

}