Example usage for javax.xml.transform TransformerFactory newInstance

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

Introduction

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

Prototype

public static TransformerFactory newInstance() throws TransformerFactoryConfigurationError 

Source Link

Document

Obtain a new instance of a TransformerFactory .

Usage

From source file:Main.java

public static String getDocumentAsXml(Document doc) {
    StringWriter sw = new java.io.StringWriter();
    try {//from  w ww .ja va2 s . c o  m
        DOMSource domSource = new DOMSource(doc);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StreamResult sr = new StreamResult(sw);
        transformer.transform(domSource, sr);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return sw.toString();
}

From source file:Main.java

public static String formatXmlAsString(Document document) {

    StringWriter writer = new StringWriter();
    TransformerFactory factory = TransformerFactory.newInstance();

    try {/*from  w w w.j a  v  a  2s  .  co  m*/

        factory.setAttribute("indent-number", new Integer(2));
    } catch (IllegalArgumentException ex) {

    }

    try {

        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

        transformer.transform(new DOMSource(document), new StreamResult(writer));
    } catch (TransformerException ex) {

        throw new RuntimeException("Error formatting xml as pretty-printed string", ex);
    }

    return writer.toString();
}

From source file:Main.java

public static String XMLDocumentToString(Document _doc) {
    TransformerFactory transfac = TransformerFactory.newInstance();
    Transformer trans;/*w  w w . j  a v a  2 s.  c o  m*/
    String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    try {
        trans = transfac.newTransformer();

        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        DOMSource source = new DOMSource(_doc);
        trans.transform(source, result);
        xmlString += sw.toString();
    } catch (TransformerConfigurationException e) {
        System.out.println("C:" + e.getClass() + "\nM:" + e.getMessage());
        return null;
    } catch (TransformerException e) {
        System.out.println("C:" + e.getClass() + "\nM:" + e.getMessage());
        return null;
    }
    return xmlString;
}

From source file:Main.java

public static String nodeToString(Node node) {
    StringWriter sw = new StringWriter();
    try {/* w w  w . j  av a 2 s .c  om*/
        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) {
        throw new RuntimeException(te);
    }
    return sw.toString();
}

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 a v  a  2  s. c  o m
    StreamResult result = new StreamResult();
    result.setOutputStream(os);

    transformer.transform(source, result);
}

From source file:Main.java

public static boolean write(String filename, Document document, boolean addDocType) {
    try {/*from www.  jav  a  2  s.  c  om*/
        TransformerFactory tf = TransformerFactory.newInstance();
        tf.setAttribute("indent-number", new Integer(4));
        Transformer transformer = tf.newTransformer();
        DOMSource source = new DOMSource(document);
        if (addDocType) {
            transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,
                    "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN");
            transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
                    "http://java.sun.com/dtd/facelet-taglib_1_0.dtd");
        }
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(source,
                new StreamResult(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename)))));
        return true;
    } catch (Exception e) {
        return false;
    }

}

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 printDOM(Node node, OutputStream out, String encoding) throws TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer;//  w  w  w.  ja  v a  2s  . c o m
    transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    transformer.transform(new DOMSource(node), new StreamResult(out));
}

From source file:Main.java

static public void outputToStream(Document document, OutputStream outputStream) throws Exception {
    // 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 String asString(Document doc) {
    try {//  w w  w . j a v a 2s.  c  om
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
        return writer.getBuffer().toString();
    } catch (TransformerException e) {
        throw Throwables.propagate(e);
    }
}