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

/** export DOM document to a file -- handy for debugging **/
public static void saveDocAsXML(Document doc, String filename) {
    try {//from ww w .  j  av a2  s .  c  o  m
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        Result output = new StreamResult(new File(filename));
        Source input = new DOMSource(doc);

        transformer.transform(input, output);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void writeXML(Document doc, String filepath) {
    try {/*  w  ww.  j a va 2s  . c  om*/
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(filepath));
        transformer.transform(source, result);
    } catch (TransformerException tfe) {
        tfe.printStackTrace();
    }
}

From source file:Main.java

public static String returnDocAsString(Document doc) throws TransformerException {
    StringWriter sw = new StringWriter();
    TransformerFactory tfFac = TransformerFactory.newInstance();
    // use null trandformation
    Transformer tf = tfFac.newTransformer();
    tf.transform(new DOMSource(doc), new StreamResult(sw));
    return sw.toString();
}

From source file:Main.java

public static String transform(String xml, File stylesheet) throws TransformerException {
    StringWriter writer = new StringWriter();
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(new StreamSource(stylesheet));
    transformer.transform(new StreamSource(xml), new StreamResult(writer));
    return writer.toString();
}

From source file:Main.java

public static String prettyPrintWithTrAX(Document document) throws TransformerException {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    StringWriter stringWriter = new StringWriter();
    StreamResult streamResult = new StreamResult(stringWriter);
    DOMSource domSource = new DOMSource(document);
    transformer.transform(domSource, streamResult);
    return stringWriter.toString();
}

From source file:Main.java

/**
 * Process a w3c XML document into a Java String.
 * //from  w  ww .j a  va  2 s. c om
 * @param outputDoc
 * @return
 */
public static String printToString(Document doc) {
    try {
        doc.normalizeDocument();
        DOMSource source = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.transform(source, result);
        return writer.toString();
    } catch (Exception e) {
        // e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static byte[] toByteArray(Node doc) {
    try {/* ww  w .  j  av a2s.co  m*/
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        StreamResult result = new StreamResult(os);
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);

        return os.toByteArray();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String toXMLString(Node node) {
    if (node == null) {
        return "";
    }/*from   www.  j a  va2 s. c  o m*/
    try {
        Transformer tran = tf.newTransformer();
        tran.setOutputProperty(OutputKeys.INDENT, "yes");
        StringWriter swriter = new StringWriter();
        Source src = new DOMSource(node);
        Result res = new StreamResult(swriter);
        tran.transform(src, res);
        return swriter.getBuffer().toString();
    } catch (Exception e) {
        e.printStackTrace();
        return "";
    }
}

From source file:Main.java

public static void writeXMLFile(Node node, String filename) throws IOException {

    try {/* w w w.  ja  v  a  2s.  com*/
        // Prepare the DOM document for writing
        DOMSource source = new DOMSource(node);
        // Prepare the output file

        StreamResult result = new StreamResult(filename);

        // Write the DOM document to the file
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.transform(source, result);

    } catch (TransformerConfigurationException e) {
        throw new IOException();
    } catch (TransformerException e) {
        throw new IOException();
    }
}

From source file:Main.java

public static void transfer(Document doc, String filepath) {
    try {/*from w ww .  java  2 s. co  m*/
        Transformer tf = tff.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(filepath);
        tf.transform(source, result);
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }

}