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 printDocument(Document doc) {
    try {//from   w  w  w.  j  av a2 s  .  c o m
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer;
        transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.transform(new DOMSource(doc),
                new StreamResult(new OutputStreamWriter(System.out, "UTF-8")));
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * //from   w  ww  .j  a  va  2 s.  com
 * @param fileName : output file
 * @param document :  xml document
 * @throws TransformerFactoryConfigurationError
 * @throws TransformerConfigurationException
 * @throws TransformerException
 */
private static void writeDocIntoFile(final String fileName, final Document document)
        throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException {
    // update the XML file
    final TransformerFactory transformerFactory = TransformerFactory.newInstance();
    final Transformer transformer = transformerFactory.newTransformer();
    final DOMSource source = new DOMSource(document);
    final StreamResult result = new StreamResult(new File(fileName));
    transformer.transform(source, result);
}

From source file:Main.java

public static File saveToFile(File file, Document document) throws TransformerException {
    // Prepare the DOM document for writing
    Source source = new DOMSource(document);

    // Prepare the output file
    Result result = new StreamResult(file.toURI().getPath());

    // Write the DOM document to the file
    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    xformer.setOutputProperty(OutputKeys.INDENT, "yes");
    xformer.transform(source, result);//from   ww w.  j  av  a2 s.  c  o m

    return file;
}

From source file:Main.java

public static String xmltoString(final Document document) throws TransformerException {
    StringWriter stringWriter = new StringWriter();
    StreamResult streamResult = new StreamResult(stringWriter);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.transform(new DOMSource(document.getDocumentElement()), streamResult);
    return stringWriter.toString();
}

From source file:Main.java

static void serialize(Document document, Writer writer) {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    transformerFactory.setAttribute("indent-number", new Integer(4));
    try {/*from w  w w .  j a  va 2  s . c om*/
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StreamResult result = new StreamResult(writer);
        transformer.transform(new DOMSource(document), result);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static void printDocument(final Document doc) {
    try {// w w  w  .  j  ava 2 s.c o m
        final TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer;
        transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.transform(new DOMSource(doc),
                new StreamResult(new OutputStreamWriter(System.out, "UTF-8")));
    } catch (final TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (final UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (final TransformerException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Convert the document to an array of bytes.
 *
 * @param doc The XML document.//from   w ww.  j  av  a2  s . co  m
 * @param encoding The encoding of the output data.
 *
 * @return The XML document as an array of bytes.
 *
 * @throws TransformerException If there is an error transforming to text.
 */
public static byte[] asByteArray(Document doc, String encoding) throws TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

    StringWriter writer = new StringWriter();
    Result result = new StreamResult(writer);
    DOMSource source = new DOMSource(doc);
    transformer.transform(source, result);
    return writer.getBuffer().toString().getBytes();
}

From source file:Main.java

public static String nodeToString(Node n, boolean isOmitXmlDeclaration, boolean isIndent)
        throws TransformerException {
    StringWriter sw = new StringWriter();
    Transformer transformer = generateTransformer(isOmitXmlDeclaration, isIndent);
    transformer.transform(new DOMSource(n), new StreamResult(sw));
    return sw.toString();
}

From source file:Main.java

/**
 * Converts an XML document to a formatted XML string.
 * /*  ww  w . j av  a 2s. com*/
 * @param doc The document to format.
 * @return Formatted XML document.
 */
public static String toString(Document doc) {
    if (doc == null) {
        return "";
    }

    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.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(domSource, result);
        return writer.toString();
    } catch (Exception e) {
        return e.toString();
    }
}

From source file:Main.java

/**
 * Save an XML document into a file/*  w  w w.  j ava 2  s  . co m*/
 *
 * @param document
 *            the XML document to save
 * @param file
 *            the file
 * @throws TransformerFactoryConfigurationError
 * @throws TransformerException
 */
public static void docToFile(final Document document, final File file)
        throws TransformerFactoryConfigurationError, TransformerException {
    final Transformer tf = TransformerFactory.newInstance().newTransformer();
    tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    tf.setOutputProperty(OutputKeys.INDENT, "yes");
    tf.transform(new DOMSource(document), new StreamResult(file));
}