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 printNode(Node node) {
    String result = "";
    try {/*from   w  ww  . ja va2  s  .  co m*/
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty("method", "xml");
        StringWriter sw = new StringWriter();
        DOMSource source = new DOMSource(node);
        transformer.transform(source, new StreamResult(sw));
        result = sw.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static void transform(InputStream xslis, InputStream xmlis, OutputStream xmlos) {
    try {//from  w  w w .  j a  v  a2s  .  c  om
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(new StreamSource(xslis));
        transformer.transform(new StreamSource(xmlis), new StreamResult(xmlos));
        xslis.close();
        xmlis.close();
        xmlos.close();
    } catch (Exception e) {
        throw new RuntimeException("Fail to do XSLT transformation", e);
    }
}

From source file:Main.java

public static String xml(Node node) {
    TransformerFactory factory = TransformerFactory.newInstance();
    return xml(factory, node);
}

From source file:Main.java

public static String documentToString(Node document) {
    try {/*  w ww . j  a va 2s .  c o  m*/
        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.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.transform(new DOMSource(document), new StreamResult(sw));
        return sw.toString().replace("\r\n", "\n");
    } catch (TransformerException e) {
        throw new IllegalAccessError("Couldn't transform document to string");
    }
}

From source file:Main.java

public static byte[] toByteArray(Node doc) {
    try {/*from  ww  w.  jav a  2s . c o m*/
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        StreamResult result = new StreamResult(os);
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);

        return os.toByteArray();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static void write2Xml(Document document) throws Exception {

    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer tf = factory.newTransformer();
    tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream(filename)));
}

From source file:Main.java

public static String xmlToString(Document doc) {
    try {/*from ww w .j av a  2s  . co m*/
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);
        return result.getWriter().toString();
    } catch (TransformerException ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:Main.java

private static void writeChangesToXml(Document doc, String xmlPath) {
    try {/*from   w  w w.  j a v  a 2s.  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

public static Templates getTemplatesByName(File xslF) {

    Templates templates = null;/*w  w  w . j  a  va 2  s .c  o  m*/
    TransformerFactory tfactory = TransformerFactory.newInstance();
    tfactory.setAttribute("http://xml.apache.org/xalan/features/incremental", java.lang.Boolean.TRUE);

    InputStream is = null;
    try {
        StreamSource ss = new StreamSource(xslF);
        is = ss.getInputStream();
        templates = tfactory.newTemplates(ss);
        if (is != null) {
            is.close();
            is = null;
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (is != null) {
                is.close();
                is = null;
            }
        } catch (Throwable t) {
            System.out.println(t);
        }
    }

    return templates;
}

From source file:Main.java

public static String elementToString(Element ele) {
    try {/*  www.  j  a  va 2 s . c o m*/
        StringWriter sw = new StringWriter();

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

        DOMSource source = new DOMSource(ele);
        StreamResult result = new StreamResult(sw);
        transformer.setOutputProperty("indent", "yes");
        transformer.transform(source, result);

        return sw.toString();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}