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

/** Returns the entire value of a node (child nodes and text) as a String
 * @param node/*from   w ww .  ja  va2s. c  o m*/
 * @return
 */
public static String nodeChildrenToString(Node node) {
    StringWriter sw = new StringWriter();
    try {
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        NodeList nl = node.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++)
            t.transform(new DOMSource(nl.item(i)), new StreamResult(sw));
    } catch (TransformerException te) {
        System.out.println("nodeToString Transformer Exception");
    }
    return sw.toString().trim();
}

From source file:Main.java

public static synchronized boolean update(String sourceXML, Document doc) {
    try {// w  w  w .ja  va 2 s  . co  m
        Transformer tf = TransformerFactory.newInstance().newTransformer();
        tf.setOutputProperty(OutputKeys.ENCODING, ENCODING);
        tf.setOutputProperty(OutputKeys.INDENT, INDENT);
        Writer out = new FileWriter(new File(sourceXML));
        tf.transform(new DOMSource(doc), new StreamResult(out));
        return true;
    } catch (TransformerException | IOException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

/** Returns the entier value of a node (child nodes and text) as a String
 * @param node/*from   www.  j  a  v a2  s . c o m*/
 * @return
 */
public static String nodeChildrenToString(Node node) {
    StringWriter sw = new StringWriter();
    try {
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        NodeList nl = node.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++)
            t.transform(new DOMSource(nl.item(i)), new StreamResult(sw));
        //      dimes.AgentGuiComm.GUICommunicator.sendLog(Level.WARNING, "", sw.toString());
    } catch (TransformerException te) {
        System.out.println("nodeToString Transformer Exception");
    }
    String test = sw.toString();
    return sw.toString().trim();
}

From source file:Main.java

/**
 * convert an XML node to an XML statement
 * @param node        current XML node//from w w w.ja v a 2 s. c o  m
 * @return            XML string
 */
public static String nodeToString(Node node) {
    //MagicBooleans.trace("nodeToString(node: " + node + ")");
    StringWriter sw = new StringWriter();
    try {
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.setOutputProperty(OutputKeys.INDENT, "no");
        t.transform(new DOMSource(node), new StreamResult(sw));
    } catch (TransformerException te) {
        System.out.println("nodeToString Transformer Exception");
    }
    String result = sw.toString();
    //MagicBooleans.trace("nodeToString() returning: " + result);
    return result;
}

From source file:Main.java

public static void transformDocument(Document document, Writer out, File stylesheet)
        throws TransformerException {
    document.normalizeDocument();//  ww w . j a  v a  2s .  c  o m
    Transformer idTransform = null;
    TransformerFactory transFactory = TransformerFactory.newInstance();
    StreamSource stylesource = new StreamSource(stylesheet);
    idTransform = transFactory.newTransformer(stylesource);
    Source source = new DOMSource(document);
    Result result = new StreamResult(out);
    idTransform.transform(source, result);
}

From source file:Main.java

/**
 * Returns the String-Representation of the given DOM-Node as well-formed DOM-Document.
 *
 * @param node DOM-Node to print/*  ww w.j a v  a 2s.  com*/
 * @param indent if true resulting XML is endented
 * @return <code>String</code> - Node as XML-String
 * @throws Exception on error
 */
public static String domNode2String(Node node, boolean indent) throws Exception {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no");

    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(node);
    transformer.transform(source, result);

    String xmlString = result.getWriter().toString();

    return xmlString;
}

From source file:Main.java

public static void pretty(Document document, OutputStream outputStream, int indent) throws IOException {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {/*from  ww w  . ja va  2s  .  c  o  m*/
        transformer = transformerFactory.newTransformer();
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException(e);
    }
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    if (indent > 0) {
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(indent));
    }
    Result result = new StreamResult(outputStream);
    Source source = new DOMSource(document);
    try {
        transformer.transform(source, result);
    } catch (TransformerException e) {
        throw new IOException(e);
    }
}

From source file:Main.java

public static String DocumentToStr(Document doc) throws TransformerException {

    StringWriter sw = new StringWriter();
    TransformerFactory tf = TransformerFactory.newInstance();
    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.transform(new DOMSource(doc), new StreamResult(sw));
    return sw.toString();

}

From source file:Main.java

/**
 * Saves the document in the given file/*from   w  w  w  .ja v a2  s.  com*/
 * @param modelDoc
 * @param fileName
 * @throws Exception
 */
public static void saveDocument(Document doc, File file) throws Exception {
    if (doc == null || file == null)
        return;

    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();

    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

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

From source file:Main.java

public static String xmlToString(Document doc) {
    try {//  w  w  w. ja v a  2s  . c om
        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;
    }
}