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 String transferXmlToString(Document doc) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {/*from w ww  .j ava 2  s.c  o  m*/
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        Properties p = t.getOutputProperties();
        p.setProperty(OutputKeys.ENCODING, "UTF-8");
        t.setOutputProperties(p);
        t.transform(new DOMSource(doc), new StreamResult(bos));
    } catch (NullPointerException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    String xmlStr = "";
    try {
        xmlStr = bos.toString("UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return xmlStr;
}

From source file:Main.java

/**
 * Get standard xml string of node//from  w w w  . j a va 2 s  . c  o  m
 * 
 * @param node
 * @return
 */
public static String nodeToString(Node node) {
    StringWriter sw = new StringWriter();
    try {
        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));
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return sw.toString();
}

From source file:Main.java

public static String convertDocumentToString(Document doc)
        throws TransformerConfigurationException, TransformerException {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();

    // Unquote below to remove XML declaration.
    // transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

    StringWriter writer = new StringWriter();

    transformer.transform(new DOMSource(doc), new StreamResult(writer));

    String output = writer.getBuffer().toString();

    return output;
}

From source file:Main.java

public static void DocumentToFile(final Document doc, File file) {
    // FileWriter writer = null;
    OutputStreamWriter outputStreamWriter = null;

    try {/*from  w w  w.j  a va  2 s  . co m*/
        // writer = new FileWriter(file);

        FileOutputStream fileOutputStream = new FileOutputStream(file);
        outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");

    } catch (Exception e) {
        System.err.println(e);

        return;
    }

    //Result l_s = new StreamResult(writer);
    Result l_s = new StreamResult(outputStreamWriter);

    doc.normalize();

    try {
        TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc), l_s);

        outputStreamWriter.close();
        //writer.close();
    } catch (Exception e) {
        System.err.println(e);

        return;
    }
}

From source file:Main.java

public static String convertXMLFileToString(String fileName) {
    try {/*from  w  w  w.  j av  a 2  s  . c om*/
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
        Document doc = documentBuilderFactory.newDocumentBuilder().parse(inputStream);
        StringWriter stw = new StringWriter();
        Transformer serializer = TransformerFactory.newInstance().newTransformer();
        serializer.transform(new DOMSource(doc), new StreamResult(stw));
        return stw.toString();
    } catch (Exception e) {
        System.out.println("Unable to load xml file: " + e.toString());
    }
    return null;
}

From source file:Main.java

public static byte[] dumpToByteArray(Document document)
        throws TransformerConfigurationException, TransformerException {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    StreamResult result = new StreamResult(bos);
    transformer.transform(new DOMSource(document), result);
    byte[] array = bos.toByteArray();

    return array;
}

From source file:Main.java

public static void transformDOMToFile(Node node, String filePath)
        throws TransformerConfigurationException, TransformerException {
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    Source source = new DOMSource(node);
    File file = new File(filePath);
    Result result = new StreamResult(file);

    transformer.transform(source, result);
}

From source file:Main.java

public static void writeXml(Document document, File outxml) {
    try {/*www. jav  a  2  s.  com*/
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer serializer;
        serializer = tf.newTransformer();
        serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        DOMSource domSource = new DOMSource(document);
        StreamResult streamResult = new StreamResult(outxml);
        serializer.transform(domSource, streamResult);
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException(e);
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * <p>/*from w w w .ja v a  2s .  c o m*/
 * This method writes a DOM document to a file
 * </p>.
 * 
 * @param doc
 *            The DOM document to write.
 * @param filename
 *            The filename to write the DOM document to.
 */
public static void writeXmlFile(Document doc, String filename) throws Exception {
    // Prepare the output file
    File file = new File(filename);
    Result result = new StreamResult(file);
    transform(doc, result);

}

From source file:Main.java

public static String toXMLString(Document doc) throws TransformerException {
    TransformerFactory transfac = TransformerFactory.newInstance();
    Transformer trans = transfac.newTransformer();
    trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    trans.setOutputProperty(OutputKeys.INDENT, "yes");
    // create string from xml tree
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    DOMSource source = new DOMSource(doc);
    trans.transform(source, result);//from ww  w  .j  av a 2s . c o m
    String xmlString = sw.toString();
    return xmlString;
}