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 Document2String(Document doc) throws IOException, TransformerException {

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final TransformerFactory tf = TransformerFactory.newInstance();
    final 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(doc), new StreamResult(new OutputStreamWriter(baos, "UTF-8")));
    return baos.toString("UTF-8");
}

From source file:Main.java

public static void writeDocument(Document document, File file) throws TransformerException {
    TransformerFactory transformFactory = TransformerFactory.newInstance();
    Transformer idTransform = transformFactory.newTransformer();
    Source input = new DOMSource(document);
    Result output = new StreamResult(file);
    idTransform.transform(input, output);
}

From source file:Main.java

public static String toString(Element element) {
    try {//from www.  j  a  va  2 s . c  o  m
        Transformer transformer = TransformerFactory.newInstance().newTransformer();

        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        StringWriter buffer = new StringWriter();
        transformer.transform(new DOMSource(element), new StreamResult(buffer));

        return buffer.toString();

    } catch (TransformerConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static String transformString(String xmlString, String xsltFilePath) throws Exception {
    StreamResult result = new StreamResult(new StringWriter());
    StreamSource s = new StreamSource(new File(xsltFilePath));
    StreamSource xml = new StreamSource(new StringReader(xmlString));

    Transformer transformer = TransformerFactory.newInstance().newTransformer(s);
    transformer.transform(xml, result);//from ww w.  jav  a  2  s  . c  o  m

    String response = result.getWriter().toString();

    return response;
}

From source file:Main.java

public static String nodeToString(Node node) throws TransformerFactoryConfigurationError, TransformerException {
    if (node == null)
        return "";
    StringWriter sw = new StringWriter();
    Transformer t = TransformerFactory.newInstance().newTransformer();
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    t.transform(new DOMSource(node), new StreamResult(sw));
    return sw.toString();
}

From source file:Main.java

public static boolean write(Document doc, String path) {
    boolean result = false;

    try {/*ww  w . j  a v  a2  s .c o  m*/
        DOMSource inputDoc = new DOMSource(doc);
        StreamResult sr = new StreamResult(path);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.transform(inputDoc, sr);
        result = true;
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }

    return result;
}

From source file:Main.java

public static String documentToString(Document doc, String xslName) {
    StringWriter sw = new StringWriter();
    try {/*from  w w w. ja va2 s  .  c o m*/
        TransformerFactory transfactory = TransformerFactory.newInstance();
        Transformer transformer = transfactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        Source source = new DOMSource(doc.getDocumentElement());
        Result result = new StreamResult(sw);
        transformer.transform(source, result);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return head + xslName + sw.toString();
}

From source file:Main.java

public static String sourceToXMLString(Source result) {

    String xmlResult = null;/*ww w.  jav a2 s. co m*/
    try {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        //transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        StringWriter out = new StringWriter();
        StreamResult streamResult = new StreamResult(out);
        transformer.transform(result, streamResult);
        xmlResult = streamResult.getWriter().toString();
    } catch (TransformerException e) {
        e.printStackTrace();
    }

    return xmlResult;
}

From source file:Main.java

public static synchronized boolean update(String sourceXML, Document doc) {
    try {//w w  w  . j a  va 2 s  .  c o  m
        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

/** Returns the entire value of a node (child nodes and text) as a String
 * @param node/*  ww  w. j  a  v  a2s. c  om*/
 * @return
 */
public static String nodeChildrenToString(Node node) {
    StringWriter sw = new StringWriter();
    try {
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        NodeList nl = node.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++)
            t.transform(new DOMSource(nl.item(i)), new StreamResult(sw));
    } catch (TransformerException te) {
        System.out.println("nodeToString Transformer Exception");
    }
    return sw.toString().trim();
}