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 serializeDocument(Document document) throws Exception {
    // Serialize XML document to String.
    StringWriter writer = new StringWriter();
    StreamResult streamResult = new StreamResult(writer);

    DOMSource domSource = new DOMSource(document);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer serializer = tf.newTransformer();
    serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.transform(domSource, streamResult);

    return writer.toString();
}

From source file:Main.java

public static OutputStream streamDocument(Document doc) throws IOException, TransformerException {
    OutputStream out = null;//from  ww w  . jav a2 s  .  c  om
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty("omit-xml-declaration", "no");
    transformer.setOutputProperty("method", "xml");
    transformer.setOutputProperty("indent", "yes");
    transformer.setOutputProperty("encoding", "UTF-8");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8")));

    return out;
}

From source file:Main.java

public static StringWriter getStringWriter(Document doc) throws TransformerException {
    DOMSource domSource = new DOMSource(doc);
    StringWriter writer = new StringWriter();
    StreamResult streamResult = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.transform(domSource, streamResult);
    return writer;
}

From source file:Main.java

public static void serialize(Source source, Writer writer, Map<String, String> properties) throws Exception {
    StreamResult sr = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = null;/*w w  w  .  j  a va  2s. c  o m*/
    t = tf.newTransformer();
    for (Map.Entry<String, String> me : properties.entrySet()) {
        t.setOutputProperty(me.getKey(), me.getValue());
    }
    t.transform(source, sr);
}

From source file:Main.java

/**
 * Transform xml Document to String/*from  w w w .ja va2  s  .c om*/
 * @param doc Xml Document
 * @return String representation
 */
public static String getContent(Document doc) {
    try {
        StringWriter sw = new StringWriter();
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

        transformer.transform(new DOMSource(doc), new StreamResult(sw));
        return sw.toString();
    } catch (Exception ex) {
        throw new RuntimeException("Error converting to String", ex);
    }
}

From source file:Main.java

/**
 * Updates existing file with Document object
 * /*www .ja v a2  s.  com*/
 * @param doc
 *            xml document
 * @param file
 *            xml file to update
 * @throws TransformerFactoryConfigurationError
 */
public static void updateXMLFile(Document doc, File file) throws TransformerFactoryConfigurationError {
    try {
        DOMSource source = new DOMSource(doc);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StreamResult result = new StreamResult(file);
        transformer.transform(source, result);
    } catch (Exception e) {
        throw new RuntimeException("Exception while saving message to xml file", e);
    }
}

From source file:Main.java

public static String documentToString(Document doc) throws TransformerException {
    // Configure the transformer
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

    // Transform the document
    DOMSource source = new DOMSource(doc);
    StringWriter stringWriter = new StringWriter();
    StreamResult streamResult = new StreamResult(stringWriter);
    transformer.transform(source, streamResult);
    return stringWriter.toString();
}

From source file:Main.java

/**
 * Method to get formatted Xml string from Xml source
 *
 * @param payload Source/*from  ww  w  . j  a  v a 2 s  .  co  m*/
 * @return a formatted Xml string
 */
public static String getXmlStringFromSource(Source payload) {
    String result = null;
    StreamResult strResult = new StreamResult(new StringWriter());

    if (payload != null) {
        try {
            TransformerFactory factory = TransformerFactory.newInstance();
            //factory.setAttribute("indent-number", new Integer(2));
            Transformer transformer = factory.newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.setOutputProperty(OutputKeys.METHOD, "xml");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "1");
            transformer.transform(payload, strResult);
        } catch (TransformerException e) {
            e.printStackTrace();
        }
        result = strResult.getWriter().toString();
    }
    return result;
}

From source file:Main.java

/**
 * Gets the node as string.//from w ww  .j a  v a 2  s  .co  m
 *
 * @param pNode
 *            the p node
 * @param encoding
 *            the encoding
 * @param pRemoveXmlLine
 *            the p remove xml line
 * @return the node as string
 * @throws Exception
 *             the exception
 */
public static String getNodeAsString(Node pNode, String encoding, boolean pRemoveXmlLine) throws Exception {
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    transformNode(pNode, encoding, pRemoveXmlLine, result);
    return writer.toString();
}

From source file:Main.java

/**
 * @param dom//  w  ww. j a  va 2 s . c o  m
 * @param outFile
 */
public static void writeDomToFile(Document dom, File outFile) {
    try {
        Transformer transformer = transFactory.newTransformer();
        DOMSource source = new DOMSource(dom);
        StreamResult result;

        result = new StreamResult(
                new OutputStreamWriter(new FileOutputStream(outFile), System.getProperty("file.encoding")));
        transformer.transform(source, result);
    } catch (Exception e) {
        throw new RuntimeException(
                "Could not write dom tree '" + dom.getBaseURI() + "' to file '" + outFile.getName() + "'!", e);
    }
}