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 elementToString(final Node node) throws Exception {
    final TransformerFactory tf = TransformerFactory.newInstance();
    final Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    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");

    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    final Document document = factory.newDocumentBuilder().newDocument();
    final Node importedNode = document.importNode(node, true);
    document.appendChild(importedNode);//from   ww w  .  j  av a 2s .  c o m
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    transformer.transform(new DOMSource(document), new StreamResult(bos));
    return bos.toString("UTF-8");
}

From source file:Main.java

private static void writeToXmlFile(Document doc, String xmlFilePath)
        throws FileNotFoundException, TransformerException {
    TransformerFactory tffactory = TransformerFactory.newInstance();
    Transformer tf = tffactory.newTransformer();
    tf.transform(new DOMSource(doc), new StreamResult(new FileOutputStream(xmlFilePath)));
}

From source file:Main.java

public static void write(Element root, String xmlPath) {
    try {//from  w  w w. j a v  a  2 s.co m
        Document doc = root.getOwnerDocument();
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        transformer.setOutputProperty("encoding", "utf-8");

        FileWriter fw = new FileWriter(xmlPath);

        transformer.transform(new DOMSource(doc), new StreamResult(fw));

        fw.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void setDocument(Document document, String fileName) {
    try {//from   w  w w  .j  a va  2 s . c o  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();
    }
}

From source file:Main.java

public static void print(Node node, OutputStream out)
        throws UnsupportedEncodingException, TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    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", "2");

    transformer.transform(new DOMSource(node), new StreamResult(new OutputStreamWriter(out, "UTF-8")));
}

From source file:Main.java

/**
 * @param dom/*  www.ja v a  2  s  .c o  m*/
 * @param outFile
 */
public static void writeDomToFile(Document dom, File outFile) {
    try {
        TransformerFactory transFact = TransformerFactory.newInstance();
        Transformer transformer = transFact.newTransformer();
        DOMSource source = new DOMSource(dom);
        StreamResult result;

        result = new StreamResult(
                new OutputStreamWriter(new FileOutputStream(outFile), System.getProperty("file.encoding")));
        transformer.transform(source, result);
    } catch (Exception e) {
        throw new RuntimeException(
                "Could not write dom tree '" + dom.getBaseURI() + "' to file '" + outFile.getName() + "'!", e);
    }
}

From source file:Utils.java

public static void writeXml(Node n, OutputStream os) throws TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    // identity/*from   w  w  w  . ja v a 2  s  .co  m*/
    Transformer t = tf.newTransformer();
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    t.transform(new DOMSource(n), new StreamResult(os));
}

From source file:Main.java

public static void print(Document doc, Writer out) throws TransformerException {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();

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

From source file:Main.java

public static void print(Element elm, OutputStream out) throws TransformerException {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();

    DOMSource source = new DOMSource(elm);
    StreamResult result = new StreamResult(out);
    transformer.transform(source, result);
}

From source file:Main.java

private static void save(Document document, OutputStream outputStream)
        throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException,
        IOException {/*  w w w.  j  a  va 2s  . c  o m*/
    DOMSource source = new DOMSource(document);
    StreamResult result = new StreamResult(outputStream);
    TransformerFactory transFactory = TransformerFactory.newInstance();
    Transformer transformer = transFactory.newTransformer();
    transformer.setOutputProperty("indent", "yes");
    transformer.transform(source, result);
    outputStream.close();
}