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

/**  This method dumps out a dom document to an output stream.
 *   Header information is turned off.//from w w w  .  jav  a  2 s  .com
 *
 *   @param doc Dom Document
 *   @param os existing outputstream you wish to write the document to.
 */
public static void DOMtoOutputStream(Document doc, OutputStream os) {
    try {
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        DOMSource src = new DOMSource(doc);
        StreamResult result = new StreamResult(os);

        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        //transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.transform(src, result);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void marshalToStream(Document doc, OutputStream ostream, boolean indent) throws Exception {
    TransformerFactory transFac = TransformerFactory.newInstance();
    Transformer trans = transFac.newTransformer();
    if (indent) {
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    }/*from www. ja  v  a  2 s  . c o m*/
    trans.setOutputProperty(OutputKeys.STANDALONE, "no");

    trans.transform(new DOMSource(doc), new StreamResult(ostream));
}

From source file:Main.java

public static String tranform(String xslSource, String original) {
    StringReader sr = new StringReader(xslSource);
    StringReader sro = new StringReader(original);
    StringWriter result = new StringWriter();
    doTransform(new StreamSource(sr), new StreamSource(sro), new StreamResult(result));
    return result.toString();
}

From source file:Main.java

public static void saveXMLDocumentAsFile(Document doc, File file) {
    try {/*from w w  w .j a va  2 s . c o  m*/
        // Indent & Doctype declaration
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

        // Prepares to write
        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(doc);

        // Output to file
        result = new StreamResult(file);
        transformer.transform(source, result);
    } catch (Exception e) {
        System.out.println("Error on saving XML file.");
        e.printStackTrace();
    }
}

From source file:Main.java

public static String toString(Element element) {
    try {// w w w. j  a v  a  2s .co m
        Transformer transformer = TransformerFactory.newInstance().newTransformer();

        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        StringWriter buffer = new StringWriter();
        transformer.transform(new DOMSource(element), new StreamResult(buffer));

        return buffer.toString();

    } catch (TransformerConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static String xmlToString(Document xml) {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = null;

    try {//from w  w w.j  a va  2  s .com
        transformer = tFactory.newTransformer();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    }
    DOMSource source = new DOMSource(xml);
    StringWriter writer = new StringWriter();
    try {
        transformer.transform(source, new StreamResult(writer));
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return writer.toString();

}

From source file:Main.java

public static void serialize(Document doc, OutputStream out) throws Exception {

    TransformerFactory tfactory = TransformerFactory.newInstance();
    Transformer serializer;/*from  w  w  w.j  a  v a 2 s . c om*/
    try {
        serializer = tfactory.newTransformer();
        //Setup indenting to "pretty print"
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        serializer.transform(new DOMSource(doc), new StreamResult(out));
    } catch (TransformerException e) {
        // this is fatal, just dump the stack and throw a runtime exception
        e.printStackTrace();

        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Parse file to xml type//from   w ww. ja  v  a  2  s  . c om
 *
 * @param transformer
 * @param source
 * @param file
 * @throws FileNotFoundException
 * @throws TransformerException
 */
public static void parseFileToXML(Transformer transformer, DOMSource source, File file)
        throws FileNotFoundException, TransformerException {
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    PrintWriter pw = new PrintWriter(file);
    StreamResult streamResult = new StreamResult(pw);
    transformer.transform(source, streamResult);
}

From source file:Main.java

/**
 * a debug method//from  w  ww  .  java  2 s . co m
 *
 * @param node            A node to be dumped to a string
 * @param omitDeclaration A boolean whether to omit the XML declaration
 * @return A string representation of the node.
 * @throws Exception If anything goes wrong. Error handling omitted.
 */
public static String dumpNode(Node node, boolean omitDeclaration) throws Exception {
    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    if (omitDeclaration) {
        xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    }
    StringWriter sw = new StringWriter();
    Result result = new StreamResult(sw);
    Source source = new DOMSource(node);
    xformer.transform(source, result);
    return sw.toString();
}

From source file:Main.java

/**
 * Write a XML document to file//ww  w  .j  a  va 2s . co  m
 * @param doc document to write
 * @param filename name of file to create
 */
public static void writeXmlFile(Document doc, String filename) {
    try {
        // Prepare the DOM document for writing
        Source source = new DOMSource(doc);

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

        // Write the DOM document to the file
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.transform(source, result);
    } catch (TransformerConfigurationException e) {
    } catch (TransformerException e) {
    }
}