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 InputStream docToInputStream(Document doc) throws TransformerException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    Source xmlSource = new DOMSource(doc);
    Result outputTarget = new StreamResult(outputStream);
    TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget);
    return new ByteArrayInputStream(outputStream.toByteArray());
}

From source file:Main.java

public static String documentToXmlString(Document document) {
    DOMSource domSource = new DOMSource(document);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer;/*from www.  j  a va2s  .c o m*/
    try {
        transformer = tf.newTransformer();
        transformer.transform(domSource, result);
    } catch (TransformerException e) {
        throw new RuntimeException(e.getMessage());
    }
    return writer.toString();
}

From source file:Main.java

static public void toHtml(Document xmldoc, OutputStream os) throws TransformerException {
    StreamResult out = new StreamResult(os);
    DOMSource domSource = new DOMSource(xmldoc);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "html");
    transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(domSource, out);
}

From source file:Main.java

public static String writeXMLtoString(org.w3c.dom.Document doc) throws Exception {
    TransformerFactory transfac = TransformerFactory.newInstance();
    Transformer trans = transfac.newTransformer();

    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    DOMSource source = new DOMSource(doc);
    trans.transform(source, result);//from  w  ww .  ja  v a2  s.  c  o m
    String xmlString = sw.toString();

    return xmlString;
}

From source file:Main.java

public static String docToString(Document doc) {
    try {//w  w  w . ja  v a2  s. c  o  m
        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);
        return writer.toString();
    } catch (TransformerException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:Main.java

public static String Doc2String(Document paramDocument) {
    try {//from   w  w w  . j  a v  a  2 s.  c om
        DOMSource localDOMSource = new DOMSource(paramDocument);
        StringWriter localStringWriter = new StringWriter();
        StreamResult localStreamResult = new StreamResult(localStringWriter);
        TransformerFactory localTransformerFactory = TransformerFactory.newInstance();
        Transformer localTransformer = localTransformerFactory.newTransformer();
        localTransformer.transform(localDOMSource, localStreamResult);
        return localStringWriter.toString();
    } catch (Exception localException) {
        localException.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String nodeToString(Node noh) throws TransformerException {
    Transformer t = TransformerFactory.newInstance().newTransformer();
    StringWriter wr = new StringWriter();
    StreamResult result = new StreamResult(wr);

    t.transform(new DOMSource(noh), result);
    return wr.toString();
}

From source file:Main.java

public static void print(Node dom) {
    try {/* w w w .j a  v a  2s.c om*/
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer t = factory.newTransformer();
        StringWriter sw = new StringWriter();
        t.transform(new DOMSource(dom), new StreamResult(sw));
        System.out.println(sw.toString());
    } catch (Exception ex) {
        System.out.println("Could not print XML document");
    }
}

From source file:Main.java

public static String domToText(Document inDocument) throws TransformerException {

    StringWriter output = new StringWriter();
    DOMSource source = new DOMSource(inDocument);
    StreamResult result = new StreamResult(output);
    transformer.transform(source, result);
    //this.getWatcher().setProgressText("saved " + getEditedFile().getAbsolutePath());

    return output.toString();

}

From source file:Main.java

public static byte[] getContent(Document doc) throws Exception {
    ByteArrayOutputStream writer = new ByteArrayOutputStream();
    transformer.transform(new DOMSource(doc), new StreamResult(new BufferedOutputStream(writer, 65536)));
    writer.flush();/*w w w. j  a  v a2s  .com*/
    return writer.toByteArray();
}