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 writeXmlFile(Document doc, File file, boolean indent, String encoding)
        throws TransformerFactoryConfigurationError, TransformerException {
    // Prepare the DOM document for writing
    Source source = new DOMSource(doc);

    // Prepare the output file
    Result result = new StreamResult(file);

    Transformer xformer = TransformerFactory.newInstance().newTransformer();

    xformer.setOutputProperty(OutputKeys.ENCODING, encoding);

    if (indent) {
        xformer.setOutputProperty(OutputKeys.INDENT, "yes");
    }//from   ww w  .  j  a  v  a  2  s.com

    xformer.transform(source, result);

}

From source file:Main.java

public static void style(Reader xsl, Reader xml, Writer out, Map params) throws TransformerException {
    Source xmlSource = new javax.xml.transform.stream.StreamSource(xml);
    TransformerFactory factory = TransformerFactory.newInstance();
    Source xslSource = new javax.xml.transform.stream.StreamSource(xsl);
    Transformer transformer;//  w ww .j  a  v  a  2  s  . co m
    transformer = factory.newTransformer(xslSource);
    if (params != null && !params.isEmpty()) {
        Iterator entries = params.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry entry = (Map.Entry) entries.next();
            transformer.setParameter((String) entry.getKey(), entry.getValue());
        }
    }
    StreamResult result = new StreamResult(out);
    transformer.transform(xmlSource, result);
}

From source file:Main.java

public static void saveDocumentToFile(Document xmlDoc, File file)
        throws TransformerConfigurationException, TransformerException {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    DOMSource source = new DOMSource(xmlDoc);
    StreamResult result = new StreamResult(file);
    transformer.transform(source, result);
}

From source file:Main.java

public synchronized static void writeXml(Document document, Writer writer, String encoding)
        throws TransformerFactoryConfigurationError, TransformerException {
    if (document != null && writer != null) {
        Source source = new DOMSource(document);
        Result result = new StreamResult(writer);
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.setOutputProperty(OutputKeys.ENCODING,
                (encoding == null || encoding.isEmpty()) ? "UTF-8" : encoding);
        xformer.transform(source, result);
    }//www  .  java 2 s.co m
}

From source file:Main.java

public static void saveDoc(Document doc, String fileName) {
    try {//from w  w w.  j  av  a2 s. c  om
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);

        StreamResult result = new StreamResult(new File(fileName));
        transformer.transform(source, result);
    } catch (TransformerException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

private static void save(Document document, OutputStream outputStream)
        throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException,
        IOException {/*from   ww w  . j a v a 2 s  .  com*/
    DOMSource source = new DOMSource(document);
    StreamResult result = new StreamResult(outputStream);
    TransformerFactory transFactory = TransformerFactory.newInstance();
    Transformer transformer = transFactory.newTransformer();
    transformer.setOutputProperty("indent", "yes");
    transformer.transform(source, result);
    outputStream.close();
}

From source file:Main.java

public static String nodeToString(Node node) {
    try {//  w w w.j  a v a 2s . co  m
        Transformer transformer = tFactory.newTransformer();
        DOMSource source = new DOMSource(node);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        transformer.transform(source, result);
        return writer.toString();
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:Main.java

/**
 * Transform a org.w3c.dom.Node to a String
 * @param node - the Node/*  ww w .j av  a  2s .  c o m*/
 * @param omitXmlDeclaration - omit XML declaration
 * @param prettyPrint - apply indentation
 * @return the String result
 * @throws TransformerException
 */
public static String elementToString(Node node, boolean omitXmlDeclaration, boolean prettyPrint) {
    String result = null;
    try {
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer();
        StringWriter buffer = new StringWriter();
        if (omitXmlDeclaration) {
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        }
        if (prettyPrint) {
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        }
        transformer.transform(new DOMSource(node), new StreamResult(buffer));
        result = buffer.toString();
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
    return result;
}

From source file:Main.java

static public void outputToStream(Document document, OutputStream outputStream) throws TransformerException {
    // Use a Transformer for output
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    //    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    DOMSource source = new DOMSource(document);
    StreamResult result = new StreamResult(outputStream);
    transformer.transform(source, result);

}

From source file:Main.java

private static String nodeToString(Node node) throws Exception {
    StringWriter sw = new StringWriter();
    Transformer t = TransformerFactory.newInstance().newTransformer();
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    t.transform(new DOMSource(node), new StreamResult(sw));
    return sw.toString();
}