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 saveToDisk(Document doc, OutputStream out)
        throws TransformerConfigurationException, TransformerException {
    Transformer serializer = TransformerFactory.newInstance().newTransformer();
    serializer.transform(new DOMSource(doc), new StreamResult(out));
}

From source file:Main.java

public static void writeDocument(Document document, File directoryToWriteTo, String fileName) {
    // Prepare the DOM document for writing
    Source source = new DOMSource(document);

    // Prepare the output file
    File file = new File(directoryToWriteTo, fileName);
    Result result = new StreamResult(file);

    // Write the DOM document to the file
    try {//from   w  w  w .j  a  va  2  s .c o m
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        transformer.transform(source, result);
    } catch (TransformerFactoryConfigurationError | TransformerException e) {
        throw new IllegalStateException(e);
    }
}

From source file:Main.java

public static String xmlToString(Document xml) throws TransformerConfigurationException, TransformerException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Transformer transformer = getTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.transform(new DOMSource(xml), new StreamResult(bos));
    return bos.toString();
}

From source file:Main.java

/**
 * Convert XML Document to a String./*w  w w .j av a  2  s .  co  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 String transText(Node doc) throws Exception {
    TransformerFactory transfactory = TransformerFactory.newInstance();
    Transformer transformer = transfactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.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 void writeDocument(Document document, File file) throws TransformerException {
    TransformerFactory transformFactory = TransformerFactory.newInstance();
    Transformer idTransform = transformFactory.newTransformer();
    Source input = new DOMSource(document);
    Result output = new StreamResult(file);
    idTransform.transform(input, output);
}

From source file:Main.java

public static String constructXMLString(Document dom) throws IOException {
    String retXMLStr = null;// w ww .  j a va 2s. c o m
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer;
    try {
        transformer = factory.newTransformer();
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        DOMSource source = new DOMSource(dom);
        transformer.transform(source, result);
        writer.close();
        retXMLStr = writer.toString();
    } catch (TransformerConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    /*
    // Make a string out of the DOM object
    System.out.println(dom.toString());
    OutputFormat format = new OutputFormat(dom);
    format.setIndenting(true);
    ByteArrayOutputStream byteoutStream = new ByteArrayOutputStream();
    XMLSerializer serializer = new XMLSerializer(byteoutStream, format);
    serializer.serialize(dom);
    String retXMLStr = new String(byteoutStream.toByteArray());
    byteoutStream.close();
     */
    return retXMLStr;

}

From source file:Main.java

public static void document2OutputStream(Document document, String encode, OutputStream os)
        throws TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    DOMSource source = new DOMSource(document);
    transformer.setOutputProperty("encoding", encode);
    transformer.setOutputProperty("indent", "yes");

    transformer.transform(source, new StreamResult(os));
}

From source file:Main.java

public static String asString(Document doc) {
    try {/*  w  w  w  .  j  ava  2  s . c  o  m*/
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
        return writer.getBuffer().toString();
    } catch (TransformerException e) {
        throw Throwables.propagate(e);
    }
}

From source file:Main.java

/**
 * @param xml/*  w  w w .jav  a2  s.c o  m*/
 */
public static void prettyPrint(String xml) {
    Source xmlInput = new StreamSource(new StringReader(xml));
    StreamResult xmlOutput = new StreamResult(new StringWriter());
    Transformer transformer = null;
    try {
        transformer = TransformerFactory.newInstance().newTransformer();
    } catch (TransformerConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "testing.dtd");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    try {
        transformer.transform(xmlInput, xmlOutput);
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String formattedxml = xmlOutput.getWriter().toString();
    System.out.println(formattedxml);

}