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

static public void outputToStream(Element document, OutputStream outputStream) throws Exception {
    // 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

public static StringBuffer transformToString(Source xmlSource, Source xslSource) {
    StringWriter writer = new StringWriter();
    Transformer transformer;/*from w ww .jav  a2 s  .  com*/
    try {
        if (xslSource == null) {
            transformer = TransformerFactory.newInstance().newTransformer();
        } else {
            transformer = TransformerFactory.newInstance().newTransformer(xslSource);
        }
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.transform(xmlSource, new StreamResult(writer));
        return writer.getBuffer();
    } catch (Exception e) {
        e.printStackTrace();
        return writer.getBuffer();
    } finally {
        try {
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

private static String toString(final Document document, final boolean indent, final boolean omitXmlDeclaration)
        throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerException {
    final Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ? "yes" : "no");
    transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    final StringWriter stringWriter = new StringWriter();
    final StreamResult streamResult = new StreamResult(stringWriter);

    transformer.transform(new DOMSource(document), streamResult);

    return stringWriter.toString();
}

From source file:Main.java

/**
 * Converts an XML file to an XSL-FO file using JAXP (XSLT).
 * @param xml the XML file// w  w w  . jav a2 s .  c o m
 * @param xslt the stylesheet file
 * @param fo the target XSL-FO file
 * @throws IOException In case of an I/O problem
 * @throws TransformerException In case of a XSL transformation problem
 */
public static void convertXML2FO(InputStream xml, File xslt, File fo) {
    //Setup output
    OutputStream out = null;
    try {
        out = new java.io.FileOutputStream(fo);
        //Setup XSLT
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(new StreamSource(xslt));

        //Setup input for XSLT transformation
        Source src = new StreamSource(xml);
        //Resulting SAX events (the generated FO) must be piped through to FOP
        Result res = new StreamResult(out);
        //Start XSLT transformation and FOP processing
        transformer.transform(src, res);
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        try {
            out.close();
        } catch (Exception e) {
        }
    }
}

From source file:Main.java

public static String toString(Document doc, String encoding, boolean indent)
        throws TransformerFactoryConfigurationError, TransformerException {
    Source source = new DOMSource(doc);
    StringWriter sw = new StringWriter();
    Result result = new StreamResult(sw);

    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.setOutputProperty(OutputKeys.ENCODING, encoding);

    if (indent) {
        xformer.setOutputProperty(OutputKeys.INDENT, "yes");
    }/*from   w ww .j a va2  s . c  o m*/

    xformer.transform(source, result);

    return sw.getBuffer().toString();
}

From source file:Main.java

static public String getPrettyPrint(Document doc) {
    try {/*from  w  ww  . ja  v  a  2 s .c o m*/
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
        return writer.getBuffer().toString();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static synchronized void createStorage() throws ParserConfigurationException, TransformerException {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

    Document doc = docBuilder.newDocument();
    Element rootElement = doc.createElement(MESSAGES);
    doc.appendChild(rootElement);//from  w ww .  j  ava 2 s  .c  o  m

    Transformer transformer = getTransformer();

    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new File(STORAGE_LOCATION));
    transformer.transform(source, result);
}

From source file:Main.java

static void formatXMLFile(String file) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(new InputSource(new InputStreamReader(new FileInputStream(file))));

    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.setOutputProperty(OutputKeys.METHOD, "xml");
    xformer.setOutputProperty(OutputKeys.INDENT, "yes");
    xformer.setOutputProperty("b;http://xml.apache.org/xsltd;indent-amount", "4");
    xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    Source source = new DOMSource(document);
    Result result = new StreamResult(new File(file));
    xformer.transform(source, result);// w ww .  java2  s  .  co  m
}

From source file:Main.java

public static String transform(Source xsltStream, Source xmlStream, String charset)
        throws TransformerException {
    try {/*from  www  .ja va2 s.  co m*/

        Transformer transformer = factory.newTransformer(xsltStream);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        transformer.transform(xmlStream, new StreamResult(outputStream));
        transformer = null;
        xmlStream = null;
        return outputStream.toString(charset);
    } catch (Exception e) {
        throw new TransformerException(e);
    }
}

From source file:Main.java

public static synchronized void createIdStorage() throws ParserConfigurationException, TransformerException {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

    Document doc = docBuilder.newDocument();
    Element rootElement = doc.createElement(IDS);
    doc.appendChild(rootElement);/* ww w .  j av  a2 s  .  c om*/

    Transformer transformer = getTransformer();

    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new File(ID_STORAGE_LOCATION));
    transformer.transform(source, result);
}