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 encodeAsXml(Node dom, OutputStream os) throws Throwable {
    try {/*from   w ww . j  a  va  2s. com*/
        DOMSource source = new DOMSource(dom);
        StreamResult result = new StreamResult(os);
        Transformer transformer;
        transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(source, result);
    } catch (TransformerConfigurationException tce) {
        Throwable x = tce;
        if (tce.getException() != null)
            x = tce.getException();
        throw x;
    } catch (TransformerException te) {
        Throwable x = te;
        if (te.getException() != null)
            x = te.getException();
        throw x;
    }
}

From source file:Main.java

public static String xmlToString(Node node) {
    try {/*  w  w  w  . j a  va2 s . co m*/
        Source source = new DOMSource(node);
        StringWriter stringWriter = new StringWriter();
        Result result = new StreamResult(stringWriter);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(source, result);
        return stringWriter.getBuffer().toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String documentToString(Document doc) {
    StreamResult sr = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(doc);
    try {//from  w  w w .ja va  2s .co  m
        _transformer.transform(source, sr);
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return sr.getWriter().toString();
}

From source file:Main.java

public static void writeDocToFile(Document doc, String filename) throws Exception {
    Source source = new DOMSource(doc);
    File file = new File(filename);
    Result result = new StreamResult(file);
    TransformerFactory tFactory = TransformerFactory.newInstance();
    tFactory.setAttribute("indent-number", 2);
    Transformer xformer = tFactory.newTransformer();
    xformer.setOutputProperty(OutputKeys.INDENT, "yes");
    //xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    xformer.transform(source, result);/*from  w ww .j av  a2  s. co  m*/

    /*OutputFormat format = new OutputFormat(doc);
    format.setIndenting(true);
    format.setIndent(2);
    Writer output = new BufferedWriter( new FileWriter(filename) );
    XMLSerializer serializer = new XMLSerializer(output, format);
    serializer.serialize(doc);*/
}

From source file:Main.java

public static String DocumentToString(final Document doc) {
    try {/*  ww  w .j a  v a2 s. co  m*/
        StringWriter writer = new StringWriter();
        Result l_s = new StreamResult(writer);

        doc.normalize();

        TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc), l_s);

        return writer.toString();
    } catch (Exception e) {
        System.err.println(e);

        return null;
    }
}

From source file:Main.java

public static String transformString(String xmlString, String xsltFilePath) throws Exception {
    StreamResult result = new StreamResult(new StringWriter());
    StreamSource s = new StreamSource(new File(xsltFilePath));
    StreamSource xml = new StreamSource(new StringReader(xmlString));

    Transformer transformer = TransformerFactory.newInstance().newTransformer(s);
    transformer.transform(xml, result);//from w  ww  . j  a va  2  s  . c o  m

    String response = result.getWriter().toString();

    return response;
}

From source file:Main.java

public static void writeXmlFile(Document doc, String fileFullPath)
        throws TransformerFactoryConfigurationError, TransformerException {
    Source source = new DOMSource(doc);
    File file = new File(fileFullPath);
    Result result = new StreamResult(file);
    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.transform(source, result);//from  www. j  a v a 2s .c o  m
}

From source file:Main.java

/** Convert an xml Document object to a String */
public static String convertDocumentToString(Document doc) {

    StreamResult sr = new StreamResult(new ByteArrayOutputStream());
    try {/* w w w .  j  av a 2s.  c o  m*/
        ourTransformer.transform(new DOMSource(doc), sr);
    } catch (TransformerException te) {
        System.out.println(te);
    }

    String docAsString = sr.getOutputStream().toString();
    return docAsString;
}

From source file:Main.java

public static String toString(Node doc) throws Exception {
    DOMSource domSource = new DOMSource(doc);
    StreamResult result = new StreamResult(new StringWriter());
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer serializer = tf.newTransformer();
    serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    //serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,"users.dtd");
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.transform(domSource, result);
    return result.getWriter().toString();
}

From source file:Main.java

public static String nodeToString(Node node) throws TransformerException {
    Source source = new DOMSource(node);
    StringWriter stringWriter = new StringWriter();
    Result result = new StreamResult(stringWriter);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(source, result);
    return stringWriter.getBuffer().toString();
}