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 void writeDocument(Document document, String location) {
    DOMSource source = new DOMSource(document);

    File file = new File(location);
    Result result = new StreamResult(file);

    Transformer xformer;//w  w  w  . j  av a2  s.co  m
    try {
        xformer = TransformerFactory.newInstance().newTransformer();

        xformer.setOutputProperty(OutputKeys.INDENT, "yes");
        xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        xformer.setOutputProperty(OutputKeys.METHOD, "xml");

        xformer.transform(source, result);
    } catch (TransformerConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:Main.java

/**
 * Writes an XML element to a given file
 * @param doc XML element/*  w  w  w  .  j  a  v  a  2s  . c  om*/
 * @param filename Filename of the file where to write XML
 */
public static void writeXmlFile(Element doc, String filename) {
    try {

        Source source = new DOMSource(doc);
        File file = new File(filename);
        Result result = new StreamResult(file);

        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.transform(source, result);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String xmlToString(Document doc) {
    try {/*  w  w w.j  a  v a 2s  .c  o  m*/
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
        String output = writer.getBuffer().toString().replaceAll("\n|\r", "");
        return output;
    } catch (TransformerException exc) {
        throw new RuntimeException("Unable to convert XML to String");
    }
}

From source file:Main.java

/**
 * Get DOM as a string//from  w  ww.  j av a2s .c o m
 * @param doc
 * @return
 */
public static String getStringFromDoc(org.w3c.dom.Document doc) {
    if (doc == null) {
        System.out.println("XML document is null");
    }
    try {
        DOMSource domSource = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
                "http://commons.omniupdate.com/dtd/standard.dtd");
        transformer.transform(domSource, result);
        writer.flush();
        return writer.toString();
    } catch (TransformerException ex) {
        System.out.println("Transformer Exception");
        // ex.printStackTrace();
        return "Error in transformation";
    }
}

From source file:Main.java

/**
 * Persists the XML to the file system./*  ww  w.  ja  va  2s  .  co  m*/
 *
 * @param src the source XML.
 * @param file the file to persist to.
 * @throws TransformerException
 */
public static void persist(Source src, File file) throws TransformerException {
    final TransformerFactory tranFactory = TransformerFactory.newInstance();
    final Transformer aTransformer = tranFactory.newTransformer();
    final Result dest = new StreamResult(file);
    aTransformer.transform(src, dest);
}

From source file:Main.java

private static void wirteXmlFile(Document doc, File file) throws TransformerException {
    // Write the content into xml file
    //logger.debug("Changes are done in xml file, Writing to the XML file");
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(file);
    transformer.transform(source, result);
}

From source file:Main.java

public static void writeDocumentToFile(Document d, File f) throws TransformerException, FileNotFoundException {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();

    DOMSource source = new DOMSource(d);
    StreamResult result = new StreamResult(new FileOutputStream(f));
    transformer.transform(source, result);
}

From source file:Main.java

public static void save(String paramString, Document paramDocument) throws Exception {
    DOMSource localDOMSource = new DOMSource(paramDocument);
    File localFile1 = new File(paramString);
    File localFile2 = localFile1.getParentFile();
    localFile2.mkdirs();/*from w  ww  .j  a  v  a2 s.  c om*/
    StreamResult localStreamResult = new StreamResult(localFile1);
    try {
        TransformerFactory localTransformerFactory = TransformerFactory.newInstance();
        Transformer localTransformer = localTransformerFactory.newTransformer();
        Properties localProperties = localTransformer.getOutputProperties();
        localProperties.setProperty("encoding", "UTF-8");
        localProperties.setProperty("indent", "yes");
        localTransformer.setOutputProperties(localProperties);
        localTransformer.transform(localDOMSource, localStreamResult);
    } catch (TransformerConfigurationException localTransformerConfigurationException) {
        localTransformerConfigurationException.printStackTrace();
    } catch (TransformerException localTransformerException) {
        localTransformerException.printStackTrace();
    }
}

From source file:Main.java

public static void write(Element root, String xmlPath) {
    try {/*from   w w w.j  av a  2 s .c  om*/
        Document doc = root.getOwnerDocument();
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        transformer.setOutputProperty("encoding", "utf-8");

        FileWriter fw = new FileWriter(xmlPath);

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

        fw.close();

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

From source file:Main.java

public static String document2XmlString(Document xmldoc) {
    try {/* ww  w. j a  va 2 s  . c o  m*/
        Source src = new DOMSource(xmldoc);
        ByteArrayOutputStream bout = new ByteArrayOutputStream();

        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(); // identity transformer
        transformer.transform(src, new StreamResult(bout));
        return bout.toString("UTF-8");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}