Example usage for javax.xml.transform Transformer transform

List of usage examples for javax.xml.transform Transformer transform

Introduction

In this page you can find the example usage for javax.xml.transform Transformer transform.

Prototype

public abstract void transform(Source xmlSource, Result outputTarget) throws TransformerException;

Source Link

Document

Transform the XML Source to a Result.

Usage

From source file:Main.java

public static synchronized boolean update(String sourceXML, Document doc) {
    try {//  w ww .ja v a2  s .c  om
        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

public static String toXMLString(Node node) {
    if (node == null) {
        return "";
    }//from   w ww .j a  v a  2  s  . co m
    try {
        Transformer tran = tf.newTransformer();
        tran.setOutputProperty(OutputKeys.INDENT, "yes");
        StringWriter swriter = new StringWriter();
        Source src = new DOMSource(node);
        Result res = new StreamResult(swriter);
        tran.transform(src, res);
        return swriter.getBuffer().toString();
    } catch (Exception e) {
        e.printStackTrace();
        return "";
    }
}

From source file:Main.java

/**
 * Writes an XML document to an output stream.
 *
 * @param document/*from   ww  w .ja va  2s.  c o m*/
 * @param outputStream
 * @throws TransformerException if the output failed
 */
public static void writeDocument(Document document, OutputStream outputStream, String encoding)
        throws TransformerException {
    Source source = new DOMSource(document);
    Result result = new StreamResult(outputStream);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty("indent", "yes"); // make the output easier to read, see Transformer.getOutputProperties
    transformer.setOutputProperty("encoding", encoding);
    transformer.transform(source, result);
}

From source file:Main.java

/**
 * Print the specified XML DOM node to the specified output stream
 * //from w  w w .jav a 2 s. c  o m
 * @param n
 *            the node to print
 * @param os
 *            the output stream to print to
 * @throws Exception
 */
public static void printNode(Node n, OutputStream os) throws Exception {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    t.transform(new DOMSource(n), new StreamResult(os));
}

From source file:Main.java

static public void outputToStream(Document document, OutputStream outputStream) throws TransformerException {
    // Use a Transformer for output
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    //    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    DOMSource source = new DOMSource(document);
    StreamResult result = new StreamResult(outputStream);
    transformer.transform(source, result);

}

From source file:Main.java

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

    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.setOutputProperty(OutputKeys.ENCODING, encoding);

    if (indent) {
        xformer.setOutputProperty(OutputKeys.INDENT, "yes");
    }//w w w  . j a v a 2s.co  m

    xformer.transform(source, result);
}

From source file:Main.java

public static String toString(Document doc) {
    try {//from w  w w . j av a 2 s .  c  o  m
        StringWriter sw = new StringWriter();
        Source source = new DOMSource(doc);
        Result result = new StreamResult(sw);

        TransformerFactory tf = TransformerFactory.newInstance();
        tf.setAttribute("indent-number", new Integer(4));
        Transformer xformer = tf.newTransformer();
        xformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        xformer.setOutputProperty(OutputKeys.INDENT, "yes");
        xformer.transform(source, result);
        return sw.toString();
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String getString(Node doc) {
    StringWriter sw = new StringWriter();

    try {/*from   w  w  w.jav a 2 s . c om*/
        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
        trans.transform(new DOMSource(doc), new StreamResult(sw));
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return sw.toString();
}

From source file:Main.java

static public void outputToStream(Element document, OutputStream outputStream) throws TransformerException {
    // Use a Transformer for output
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    //     transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    DOMSource source = new DOMSource(document);
    StreamResult result = new StreamResult(outputStream);
    transformer.transform(source, result);

}

From source file:Signing.java

private static void dumpDocument(Node root) throws TransformerException {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(new DOMSource(root), new StreamResult(System.out));
    }// w w w.  j  a  va2 s.  c  o  m