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 formatXmlAsStringNoWhitespace(Document document) {

    StringWriter writer = new StringWriter();
    TransformerFactory factory = TransformerFactory.newInstance();

    try {/*from   w w  w . j  a  v  a  2 s. co m*/

        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "no");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");

        transformer.transform(new DOMSource(document), new StreamResult(writer));
    } catch (TransformerException ex) {

        throw new RuntimeException("Error formatting xml as no-whitespace string", ex);
    }

    return writer.toString();
}

From source file:Main.java

/**
 * Serialize W3C document into given output stream
 * @param doc W3C document/*from   ww w. jav  a  2 s  . com*/
 * @param out OutputStream 
 * @throws TransformerException
 */
public static void print(Document doc, OutputStream out) throws TransformerException {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();

    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(out);
    transformer.transform(source, result);
}

From source file:Main.java

/**
 * Creates XML from W3C Document from the xml.
 *
 * @param document the xml that needs to be converted.
 * @return the XML.//from   w ww.j  av  a2s . c o  m
 * @throws Exception is there is some error during operation.
 */
public static String createXml(Document document) throws Exception {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    StringWriter stringWriter = new StringWriter();
    StreamResult result = new StreamResult(stringWriter);
    DOMSource source = new DOMSource(document);
    transformer.transform(source, result);
    return stringWriter.toString();
}

From source file:Main.java

/**
 * Returns a string representation of {@code node}. This method should only
 * be used for debugging purposes, at it creates a new transformer factory
 * and transformer upon each call./*from  www.j a v  a2  s  .com*/
 * 
 * @param n
 *            the node
 * @return a string representation of {@code node}
 */
public static String toString(Node n) {
    try {
        Transformer t = TransformerFactory.newInstance().newTransformer();
        StringWriter sw = new StringWriter();
        t.transform(new DOMSource(n), new StreamResult(sw));
        return sw.toString();
    } catch (TransformerException e) {
        return null;
    }
}

From source file:Main.java

public static void save(Document document) throws TransformerConfigurationException, TransformerException,
        TransformerFactoryConfigurationError, SAXException, IOException, ParserConfigurationException {
    TransformerFactory.newInstance().newTransformer().transform(new DOMSource(document),
            new StreamResult(PATH));
}

From source file:Main.java

public static String document2String(Document document, String encode) throws TransformerException {
    String xml = null;/*from   w ww .j ava  2 s .co m*/
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    DOMSource source = new DOMSource(document);
    transformer.setOutputProperty("encoding", encode);
    transformer.setOutputProperty("indent", "yes");
    StringWriter sw = new StringWriter();
    transformer.transform(source, new StreamResult(sw));
    xml = sw.toString();
    return xml;
}

From source file:Main.java

/**
 * Method to transform an XML document into a pretty-formatted string.
 * @param xml//from   w  ww . j  a  va2s.  c  om
 * @return
 * @throws Exception
 */
public static final String xmlToString(Document xml) throws Exception {
    Transformer tf = TransformerFactory.newInstance().newTransformer();
    tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    tf.setOutputProperty(OutputKeys.INDENT, "yes");
    Writer out = new StringWriter();
    tf.transform(new DOMSource(xml), new StreamResult(out));
    return out.toString();
}

From source file:Main.java

public static String createPrintableString(Document doc) {
    String s = "";
    try {//from  w w w.  ja  va 2 s. c  o m
        // set up a transformer
        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);

        s = sw.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return s;
}

From source file:Main.java

private static String getStringFromDocument(Node doc) {
    try {//from   ww w. ja  va 2  s  .  co m
        DOMSource domSource = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.transform(domSource, result);
        return writer.toString();
    } catch (TransformerException ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static void dumpElement(Logger log, Element elem) {

    try {/*from  w  w  w. ja  va  2 s . co  m*/
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer();
        StringWriter buffer = new StringWriter();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.transform(new DOMSource(elem), new StreamResult(buffer));

        log.debug(buffer.toString());
    } catch (TransformerFactoryConfigurationError | IllegalArgumentException | TransformerException ex) {
        log.error("Failed to dump element", ex);
    }
}