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

private static void writeTreeToXMLFile() {

    try {// ww w.  j av a2 s  .  co  m
        Source source = new DOMSource(dom);

        File file = new File("resources/Styles.xml");
        Result result = new StreamResult(file);

        Transformer xformer = TransformerFactory.newInstance().newTransformer();

        xformer.transform(source, result);
        System.out.println("End of write method");

    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static void buildXmlFile(Document doc, String path) {
    TransformerFactory tfactory = TransformerFactory.newInstance();
    try {//from w  ww . j  av  a  2s.c o m
        Transformer transformer = tfactory.newTransformer();
        DOMSource source = new DOMSource(doc);

        StreamResult result = new StreamResult(new File(path));
        transformer.setOutputProperty("encoding", "gb2312");
        transformer.transform(source, result);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void documentToFile(Document doc, File file, Charset charset) throws IOException {
    StreamResult sr = new StreamResult(new OutputStreamWriter(new FileOutputStream(file), charset));
    DOMSource source = new DOMSource(doc);
    try {/*  w  w w .jav a2 s  .  c  o m*/
        _transformer.transform(source, sr);
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:Main.java

public static String writeXmlToString(Document doc) {
    String result = "";
    Result streamResult = new StreamResult(result);
    writeXmlToStream(doc, streamResult);
    return result;
}

From source file:Main.java

public static String xmlToString(Document doc) throws Exception {

    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(doc.getDocumentElement()), new StreamResult(writer));
    String result = writer.toString();
    System.out.println(result);/*  w  ww. j a  va 2s. c  om*/
    return result;
}

From source file:Main.java

public static boolean debug(Node paramNode) {
    try {//from   ww  w. j  a  va2  s. c  om
        TransformerFactory localTransformerFactory = TransformerFactory.newInstance();
        Transformer localTransformer = localTransformerFactory.newTransformer();
        DOMSource localDOMSource = new DOMSource(paramNode);
        StreamResult localStreamResult = new StreamResult(System.out);
        localTransformer.transform(localDOMSource, localStreamResult);
        return true;
    } catch (Exception localException) {
        localException.printStackTrace(System.out);
    }
    return false;
}

From source file:Main.java

public static void SaveDom(Document dom, String filepath) {
    try {//from   w  ww .  j  av  a  2 s.  c  o  m
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();

        DOMSource domSource = new DOMSource(dom);

        StreamResult streamResult = new StreamResult(new FileOutputStream(filepath));
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        transformer.transform(domSource, streamResult);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static synchronized void writeXmlFile(String xmlFile, Document document) throws Exception {
    // transform the document to the file
    TransformerFactory xformFactory = TransformerFactory.newInstance();
    Transformer idTransformer = xformFactory.newTransformer();
    idTransformer.setOutputProperty("indent", "yes");
    Source input = new DOMSource(document);
    Result output = new StreamResult(new File(xmlFile));
    idTransformer.transform(input, output);
    /*//from   w  ww . j a va 2  s  . c  o m
     OutputFormat format = new OutputFormat();
     format.setLineSeparator(LineSeparator.Unix);
     format.setIndenting(true);
     format.setLineWidth(0);
     format.setPreserveSpace(true);
     XMLSerializer ser = new XMLSerializer(new FileWriter(xmlFile),
     format);
     ser.asDOMSerializer();
     ser.serialize(document);
     */
    return;
}

From source file:Main.java

public static boolean write(Document doc, String path) {
    boolean result = false;

    try {//from w w w  .j  a  v  a2 s. co m
        DOMSource inputDoc = new DOMSource(doc);
        StreamResult sr = new StreamResult(path);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.transform(inputDoc, sr);
        result = true;
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }

    return result;
}

From source file:Main.java

public static String doc2str(Document doc) throws TransformerConfigurationException, TransformerException {
    StreamResult result = new StreamResult(new StringWriter());
    doc2stream(doc, result);//from  w w  w  .  j av a2  s. c o  m
    String xmlString = result.getWriter().toString();
    return xmlString;
}