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 docToString(Document doc) {
    try {//from ww  w  .  j  a  v  a 2s  .c o m
        DOMSource domSource = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.transform(domSource, result);
        return writer.toString();
    } catch (TransformerException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:Main.java

public static String document2XmlString(Document xmldoc) {
    try {//  w  ww  .  ja v a 2s. com
        Source src = new DOMSource(xmldoc);
        ByteArrayOutputStream bout = new ByteArrayOutputStream();

        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(); // identity transformer
        transformer.transform(src, new StreamResult(bout));
        return bout.toString("UTF-8");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static void printDocument(Document doc) {
    try {//www .ja v a2 s  . c o m
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer 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(System.out, "UTF-8")));
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void doc2stream(Document doc, StreamResult result)
        throws TransformerConfigurationException, TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    DOMSource source = new DOMSource(doc);
    transformer.transform(source, result);
}

From source file:Main.java

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

    Transformer transformer = TransformerFactory.newInstance().newTransformer(s);
    transformer.transform(xml, result);// w w w. ja  v  a2s  .co  m

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

    return response;
}

From source file:Main.java

/**
 * Transform xml Document to String/*from  w ww.ja v  a  2s . c om*/
 * @param doc Xml Document
 * @return String representation
 */
public static String getContent(Document doc) {
    try {
        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.transform(new DOMSource(doc), new StreamResult(sw));
        return sw.toString();
    } catch (Exception ex) {
        throw new RuntimeException("Error converting to String", ex);
    }
}

From source file:Main.java

public static String toXmlString(Document doc) throws Exception {
    DOMSource domSource = new DOMSource(doc);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.transform(domSource, result);
    writer.flush();/* w w w . j a  v  a 2  s  .c  o  m*/

    return writer.toString();
}

From source file:Main.java

public static String constructXMLString(Document dom) throws IOException {
    String retXMLStr = null;/*  ww w . j av  a 2 s . c  om*/
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer;
    try {
        transformer = factory.newTransformer();
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        DOMSource source = new DOMSource(dom);
        transformer.transform(source, result);
        writer.close();
        retXMLStr = writer.toString();
    } catch (TransformerConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    /*
    // Make a string out of the DOM object
    System.out.println(dom.toString());
    OutputFormat format = new OutputFormat(dom);
    format.setIndenting(true);
    ByteArrayOutputStream byteoutStream = new ByteArrayOutputStream();
    XMLSerializer serializer = new XMLSerializer(byteoutStream, format);
    serializer.serialize(dom);
    String retXMLStr = new String(byteoutStream.toByteArray());
    byteoutStream.close();
     */
    return retXMLStr;

}

From source file:Main.java

public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {
    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(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8")));
}

From source file:Main.java

public static String toXMLString(Document doc) throws TransformerException {
    TransformerFactory transfac = TransformerFactory.newInstance();
    Transformer trans = transfac.newTransformer();
    trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    trans.setOutputProperty(OutputKeys.INDENT, "yes");
    // create string from xml tree
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    DOMSource source = new DOMSource(doc);
    trans.transform(source, result);//from  w ww. ja  v  a 2  s .c  o m
    String xmlString = sw.toString();
    return xmlString;
}