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

/**
 * Save the XML document to a file.//from   www  .ja v  a2 s.c o m
 *
 * @param doc The XML document to save.
 * @param file The file to save the document to.
 * @param encoding The encoding to save the file as.
 *
 * @throws TransformerException If there is an error while saving the XML.
 */
public static void save(Document doc, String file, String encoding) throws TransformerException {
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        //        initialize StreamResult with File object to save to file

        Result result = new StreamResult(new File(file));
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);
    } finally {

    }
}

From source file:Main.java

public static void transformDocument(Document doc, String xsl, String targetFile) {
    try {/*from  www . j  a v  a  2s.co m*/
        Source source = new DOMSource(doc);

        // Creation of the output file
        File file = new File(targetFile);
        Result result = new StreamResult(file);

        // configuration of the transformer
        TransformerFactory factoryT = TransformerFactory.newInstance();
        StreamSource stylesource = new StreamSource(xsl);
        Transformer transformer;

        transformer = factoryT.newTransformer(stylesource);
        transformer.setOutputProperty(OutputKeys.METHOD, "text");

        // Transformation
        transformer.transform(source, result);
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

/**
 * print document to string//from   www .  j  a  v a2s  .  c om
 * @param doc
 * @return
 * @throws TransformerException
 */
public static String toString(Document doc) throws TransformerException {
    Transformer tf = tff.newTransformer();
    StringWriter writer = new StringWriter();
    tf.transform(new DOMSource(doc), new StreamResult(writer));
    String output = writer.getBuffer().toString();
    return output;
}

From source file:Main.java

public static String docToString(Document doc, boolean formated) {

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {/*  w  ww. j a  v  a  2s .  c om*/
        transformer = tf.newTransformer();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
        return null;
    }

    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    if (formated) {
        // linefeed formatting
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    } else {
        // remove xml header
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    }
    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);

    DOMSource domSource = new DOMSource(doc);
    try {
        transformer.transform(domSource, sr);
    } catch (TransformerException e) {
        e.printStackTrace();
        return null;
    }
    return sw.toString();
}

From source file:Main.java

/**
 * <p>/*from www .j  a  v a  2 s  .c  om*/
 * Converts a {@link Document} to a {@link InputStream} that can be returned
 * from {@link PersistenceHandler #save(ExerciseData) save()} [ eg. return
 * XMLHelper.xmlToInputStream(document-containing-exer-data); ]
 * </p>
 * 
 * @param doc
 *            {@link Document} to be converted to a stream
 * @return {@link InputStream} generated from the {@link Document}
 * @throws TransformerConfigurationException
 * @throws TransformerException
 * @throws TransformerFactoryConfigurationError
 */
public static byte[] xmlToBytes(Document doc)
        throws TransformerConfigurationException, TransformerException, TransformerFactoryConfigurationError {

    DOMSource source = new DOMSource(doc);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    Result res = new StreamResult(outputStream);

    TransformerFactory.newInstance().newTransformer().transform(source, res);

    return outputStream.toByteArray();

}

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();
}

From source file:edu.unc.lib.dl.util.SOAPUtil.java

public static void print(SOAPMessage msg, OutputStream out) {
    StreamResult result = new StreamResult(out);
    print(msg, result);
}

From source file:Main.java

/**
 * @param sourceXML the XML file to transform
 * @param xslt the XSL stylesheet to use for transformation
 * @param targetXML the result of XSL transformation
 * @throws TransformerException if an error occurs configuraing or applying the transformation
 *//*w  w w .  j a v  a2 s .  c  om*/
public static void transform(final File sourceXML, final File xslt, final File targetXML)
        throws TransformerException {
    //      logger.debug("sourceXML = " + sourceXML);
    //      logger.debug("xslt = " + xslt);
    //      logger.debug("targetXML = " + targetXML);
    Transformer transformer = createTransformer(xslt, Collections.<String, String>emptyMap());
    Source source = new StreamSource(sourceXML);
    Result result = new StreamResult(targetXML);
    transformer.transform(source, result);
}

From source file:Main.java

private static void updateNode(String nodeName, String value) throws Exception {
    File f = new File(System.getProperty("user.dir") + "\\DBConf.xml");
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(f);
    Node tempElem = doc.getElementsByTagName(nodeName).item(0);
    tempElem.getFirstChild().setNodeValue(value);
    doc.getElementsByTagName("conf").item(0).appendChild(tempElem);
    DOMSource source = new DOMSource(doc);
    StreamResult res = new StreamResult(new FileOutputStream(f));
    TransformerFactory.newInstance().newTransformer().transform(source, res);

}

From source file:Main.java

public static void transformDocument(final Document doc, final String xsl, final String targetFile) {
    try {/*w  w w.  j a  va 2  s.c  om*/
        final Source source = new DOMSource(doc);

        // Creation of the output file
        final File file = new File(targetFile);
        final Result result = new StreamResult(file);

        // configuration of the transformer
        final TransformerFactory factoryT = TransformerFactory.newInstance();
        final StreamSource stylesource = new StreamSource(xsl);
        Transformer transformer;

        transformer = factoryT.newTransformer(stylesource);
        transformer.setOutputProperty(OutputKeys.METHOD, "text");

        // Transformation
        transformer.transform(source, result);
    } catch (final TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (final TransformerException e) {
        e.printStackTrace();
    }

}