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 nodeAsString(Node node) {
    String nodeStr = "";
    TransformerFactory tff = TransformerFactory.newInstance();
    try {//from   w  w w .  ja  va 2 s  .  c  o  m
        Transformer tf = tff.newTransformer();
        tf.setOutputProperty("encoding", "UTF-8");

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        tf.transform(new DOMSource(node), new StreamResult(bos));
        return bos.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return nodeStr;
}

From source file:Main.java

private static void writeDo(Document doc, Writer w, String encoding)
        throws TransformerFactoryConfigurationError, TransformerException {
    Source source = new DOMSource(doc);
    Result result = new StreamResult(w);

    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.setOutputProperty(OutputKeys.ENCODING, encoding);
    xformer.transform(source, result);/*from   w w w  .  j a  va  2 s  . co  m*/

}

From source file:Main.java

public static void write(Document doc, Writer writer)
        throws TransformerConfigurationException, TransformerException {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(writer);
    transformer.transform(source, result);
}

From source file:Main.java

public static void writeXmlFile(Document file, String fileName) throws TransformerException {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    DOMSource source = new DOMSource(file);
    StreamResult result = new StreamResult(new File(fileName));
    transformer.transform(source, result);
}

From source file:Main.java

/**
 * Transforms a DOM node to a String./*from  ww  w  .j  a v  a  2 s  . c o  m*/
 *
 * @param node the node
 * @return the transformed XML representation
 * @throws TransformerException if a transformation error occurs.
 */
public static String transform(Node node) throws TransformerException {
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    Source source = new DOMSource(node);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.transform(source, result);
    return writer.toString();
}

From source file:Main.java

public static void write2Xml(Document document) throws FileNotFoundException, TransformerException {

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(new DOMSource(document), new StreamResult(new FileOutputStream(filename)));
}

From source file:Main.java

/**
 * Save a DOM document//from  w  w w  . j  a  va  2  s .c  o  m
 */
public static void saveDocument(Document document, OutputStream out, boolean indent) throws IOException {
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        if (indent) {
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        }
        transformer.transform(new DOMSource(document), new StreamResult(out));
    } catch (TransformerException e) {
        throw new IOException(e.getMessage());
    }
}

From source file:Main.java

public static String nodeToString(Node node) {
    StringWriter sw = new StringWriter();
    try {/*  w w  w .ja  v  a2s  . c o m*/
        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 te) {
        System.err.println("nodeToString Transformer Exception");
    }
    return sw.toString();
}

From source file:Main.java

/**
 * Serialize XML from a {@link Document} to an OutputStream.
 * @param doc/*from w w  w .j  a va  2 s . c o m*/
 * @param out
 */
public static void serialiseXml(Document doc, Writer out) {
    DOMSource domSource = new DOMSource(doc.getDocumentElement());
    StreamResult streamResult = new StreamResult(out);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer serializer = null;
    try {
        serializer = tf.newTransformer();
    } catch (TransformerConfigurationException e) {
    }
    serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "users.dtd");
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    try {
        serializer.transform(domSource, streamResult);
    } catch (TransformerException e) {
    }
}

From source file:Main.java

public static void setDocument(Document document, String fileName) {
    try {/*w  w  w.  ja v a 2 s .co m*/
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource domSource = new DOMSource(document);
        StreamResult streamResult = new StreamResult(new File(fileName));
        transformer.transform(domSource, streamResult);

    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
}