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 writeDocumentToString(Document document) {
    try {/*from   w w  w. j ava 2s.  c  o  m*/
        TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans;
        trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        StringWriter writer = new StringWriter();
        try {
            StreamResult streamResult = new StreamResult(writer);
            DOMSource source = new DOMSource(document);
            trans.transform(source, streamResult);
        } catch (TransformerException e) {
            e.printStackTrace();
            return null;
        } finally {
            writer.close();
        }
        return writer.toString();

    } catch (TransformerConfigurationException | IOException e) {
        e.printStackTrace();
        return null;
    }

}

From source file:Main.java

public static String xmlToString(Document doc) {
    try {/*from w  w w .ja  va  2  s.com*/
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);
        return result.getWriter().toString();
    } catch (TransformerException ex) {
        System.err.println("Error: Cannot load xml transformer");
        System.err.println("Cause: " + ex.toString());
        System.exit(1);
        return null;
    }
}

From source file:Main.java

/**
 * Convert the document to an array of bytes.
 * //w w  w.  j av  a2 s.  c  om
 * @param doc
 *            The XML document.
 * @param encoding
 *            The encoding of the output data.
 * 
 * @return The XML document as an array of bytes.
 * 
 * @throws TransformerException
 *             If there is an error transforming to text.
 */
public static byte[] asByteArray(Document doc, String encoding) throws TransformerException {
    final Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

    final StringWriter writer = new StringWriter();
    final Result result = new StreamResult(writer);
    final DOMSource source = new DOMSource(doc);
    transformer.transform(source, result);
    return writer.getBuffer().toString().getBytes();
}

From source file:Main.java

private static byte[] serializeXML(Transformer transformer, Source input)
        throws UnsupportedEncodingException, TransformerException {
    StringWriter stringWriter = new StringWriter();
    Result result = new StreamResult(stringWriter);
    transformer.transform(input, result);

    String streamString = stringWriter.getBuffer().toString();
    byte[] byteArray = streamString.getBytes("UTF-8");

    return byteArray;
}

From source file:Main.java

public static void xmlToStream(Node n, OutputStream os) throws Exception {
    Source source = new DOMSource(n);

    //  PrintWriter pr = new PrintWriter(new OutputStreamWriter(os,StandardCharsets.UTF_8),true);
    Result result = new StreamResult(os);//pr);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.transform(source, result);
}

From source file:Main.java

/**
 * Writes the XML document to the particular file specified as argument
 * //from  ww  w.  ja v a 2 s. co m
 * @param doc the document
 * @param filename the path to the file in which to write the XML data writing
 *            operation fails
 */
public static void writeXMLDocument(Document doc, String filename) {
    try {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer;
        transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(filename));
        transformer.transform(source, result);
        log.fine("writing operation to " + filename + " successful!");
    } catch (TransformerConfigurationException e) {
        log.warning(e.getMessage());
    } catch (TransformerException e) {
        log.warning(e.getMessage());
    }
}

From source file:Main.java

public static String XMLtoString(Document doc) throws Exception {
    try {/*from  w  ww. ja v a2 s . c  o  m*/
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer;
        transformer = tf.newTransformer();
        // below code to remove XML declaration
        // 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 te) {
        throw new Exception(te.getMessageAndLocation());
    }
}

From source file:Main.java

private static String xmlToString(Node node) {
    try {//from   w ww .j  a v  a2s . c  o  m
        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 transformXmlToString(Document importPackageDocument)
        throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException,
        IOException {/*from   w  w w.  j  ava  2 s.c  o m*/
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    StringWriter writer = new StringWriter();
    javax.xml.transform.Result result = new StreamResult(writer);
    Source source = new DOMSource(importPackageDocument);
    transformer.transform(source, result);
    writer.close();
    String xml = writer.toString();
    return xml;
}

From source file:Main.java

/**
 * To output a Node as a String.//from   ww w  .j  ava 2 s  .  c  om
 */
public static String nodeToString(Node document) throws Exception {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    Source xmlSource = new DOMSource(document);
    Result outputTarget = new StreamResult(outputStream);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.transform(xmlSource, outputTarget);

    return outputStream.toString();
}