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

/**
 * *****************************************
 * Convert XML document to string/*from   w w  w .j  a v  a 2 s. c om*/
 * ******************************************.
 *
 * @param xmlDocument the xml document
 * @return the string
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws TransformerException the transformer exception
 */
public static String converXmlDocToString(Document xmlDocument) throws IOException, TransformerException {
    String xmlString = "";

    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");

    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(xmlDocument), new StreamResult(writer));
    xmlString = writer.getBuffer().toString().replaceAll("\n|\r", "");
    return xmlString;
}

From source file:Main.java

public static String xmlDocToString(Document document) throws TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(document), new StreamResult(writer));
    return writer.getBuffer().toString();
}

From source file:Main.java

public static String xmlToString(Node doc) {
    try {/*from  w  w w. ja v a  2 s.  c om*/
        DOMSource domSource = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(domSource, result);
        return writer.toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String xmlToString(Document doc) {
    try {/*  w  w  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) {
        ex.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String elementToString(Element ele) {
    try {/*from ww  w.  j a va  2  s.  co  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;
    }
}

From source file:Main.java

static public String getPrettyPrint(Document doc) {
    try {//w  ww  .  ja  v a 2s  .c o  m
        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 (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static byte[] dumpToByteArray(Document document)
        throws TransformerConfigurationException, TransformerException {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    StreamResult result = new StreamResult(bos);
    transformer.transform(new DOMSource(document), result);
    byte[] array = bos.toByteArray();

    return array;
}

From source file:Main.java

/**
 * Creates XML from W3C Document from the xml.
 *
 * @param document the xml that needs to be converted.
 * @return the XML./*  www.  ja  va 2  s . c o m*/
 * @throws Exception is there is some error during operation.
 */
public static String createXml(Document document) throws Exception {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    StringWriter stringWriter = new StringWriter();
    StreamResult result = new StreamResult(stringWriter);
    DOMSource source = new DOMSource(document);
    transformer.transform(source, result);
    return stringWriter.toString();
}

From source file:Main.java

public static String documentToString(Document doc) throws TransformerException {
    // Configure the transformer
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

    // Transform the document
    DOMSource source = new DOMSource(doc);
    StringWriter stringWriter = new StringWriter();
    StreamResult streamResult = new StreamResult(stringWriter);
    transformer.transform(source, streamResult);
    return stringWriter.toString();
}

From source file:Main.java

public static void serialize(Source source, Writer writer, Map<String, String> properties) throws Exception {
    StreamResult sr = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = null;/*from  ww w .  j a v  a2s.c  o  m*/
    t = tf.newTransformer();
    for (Map.Entry<String, String> me : properties.entrySet()) {
        t.setOutputProperty(me.getKey(), me.getValue());
    }
    t.transform(source, sr);
}