Example usage for javax.xml.transform TransformerFactory newTransformer

List of usage examples for javax.xml.transform TransformerFactory newTransformer

Introduction

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

Prototype

public abstract Transformer newTransformer() throws TransformerConfigurationException;

Source Link

Document

Create a new Transformer that performs a copy of the Source to the Result .

Usage

From source file:Main.java

public static String saveXmlToStreamWriter(Document doc) throws TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(writer));

    return writer.getBuffer().toString();
}

From source file:Main.java

/**
 * Saves a given XML document to the given output stream.
 *///from  w ww  .j  ava  2s. co m
public static void writeXML(Document document, OutputStream os) throws IOException {
    DOMSource src = new DOMSource(document);
    StreamResult res = new StreamResult(os);
    TransformerFactory tf = TransformerFactory.newInstance();
    try {
        Transformer t = tf.newTransformer();
        t.transform(src, res);
    } catch (TransformerException e) {
        throw new IOException(e.getMessage());
    }
}

From source file:Main.java

public static void writeToFile(Document doc, String file) throws Exception {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    DOMSource source = new DOMSource(doc);
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
    StreamResult result = new StreamResult(pw);
    transformer.transform(source, result);
}

From source file:Main.java

public static String transText(Node doc) throws Exception {
    TransformerFactory transfactory = TransformerFactory.newInstance();
    Transformer transformer = transfactory.newTransformer();
    transformer.setOutputProperty("omit-xml-declaration", "yes");
    transformer.setOutputProperty("indent", "yes");

    Source source = new DOMSource(doc);
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    transformer.transform(source, result);
    return sw.toString();
}

From source file:Main.java

/**
 * Save document to a file/*from   w  ww . ja  va  2s  . com*/
 *
 * @param document : Document to be saved
 * @param fileName : Represent file name to save
 * @throws Exception
 */
public static void saveDocumentTo(Document document, String fileName) throws Exception {
    File encryptionFile = new File(fileName);
    try (FileOutputStream fOutStream = new FileOutputStream(encryptionFile)) {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(fOutStream);
        transformer.transform(source, result);
    }
}

From source file:Main.java

public static void writeXml(OutputStream os, Node node, String encoding) throws TransformerException {
    TransformerFactory transFactory = TransformerFactory.newInstance();
    Transformer transformer = transFactory.newTransformer();
    transformer.setOutputProperty("indent", "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, encoding);

    DOMSource source = new DOMSource();
    source.setNode(node);//from w  w  w . j  ava  2  s  .  com
    StreamResult result = new StreamResult();
    result.setOutputStream(os);

    transformer.transform(source, result);
}

From source file:Main.java

private static void writeChangesToXml(Document doc, String xmlPath) {
    try {/*  ww w. ja  v  a 2 s.c  o m*/
        DOMSource source = new DOMSource(doc);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        StreamResult result = new StreamResult(xmlPath);
        transformer.transform(source, result);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

/** Write a DOM document to an OutputStream */
public static void writeXmlDocumentToStream(Document document, OutputStream stream) throws Exception {
    Source source = new DOMSource(document);
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    StreamResult result = new StreamResult(stream);
    transformer.transform(source, result);
    stream.flush();//from  w  ww.ja v a  2 s .  c  o m
}

From source file:Main.java

public static void writeDocument(OutputStream iStream, Element iElement) throws TransformerException {
    TransformerFactory lTransformFactory = TransformerFactory.newInstance();
    Transformer lTransformer = lTransformFactory.newTransformer();

    DOMSource lSource = new DOMSource(iElement);
    StreamResult lStreamResult = new StreamResult(iStream);
    lTransformer.transform(lSource, lStreamResult);
}

From source file:Main.java

public static String getXMLString(Node doc) throws TransformerException, TransformerConfigurationException {
    TransformerFactory tranFactory = TransformerFactory.newInstance();
    Transformer aTransformer = tranFactory.newTransformer();

    StringWriter sw = new StringWriter();

    Source src = new DOMSource(doc);
    Result dest = new StreamResult(sw);

    aTransformer.transform(src, dest);/*from   ww w  . j  av  a  2  s  .  c  om*/

    return sw.toString();
}