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

/**
 * Makes the specified XML string pretty.
 * /*w  w  w  .j a va 2  s. c o  m*/
 * @param xmlString
 *            The XML string to process.
 * @return Pretty-printed XML string.
 */
public static final String prettyPrint(final String xmlString) {
    try (InputStream inputStream = new ByteArrayInputStream(xmlString.getBytes());
            ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        final DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        final Document document = documentBuilder.parse(inputStream);

        final TransformerFactory tfactory = TransformerFactory.newInstance();
        final Transformer serializer = tfactory.newTransformer();
        // Setup indenting to "pretty print"
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        final DOMSource xmlSource = new DOMSource(document);
        final StreamResult outputTarget = new StreamResult(baos);
        serializer.transform(xmlSource, outputTarget);
        return baos.toString("utf-8");
    } catch (ParserConfigurationException | TransformerException | SAXException | IOException ex) {
        throw new RuntimeException("Can't pretty print xml!", ex);
    }
}

From source file:Main.java

/**
 * convert node object into String object.
 * @param node/*from  w ww. jav  a 2 s  .  com*/
 * @param omitXmlDecl
 * @return
 */
public static String dom2String(Node node, boolean omitXmlDecl) {
    Source source = new DOMSource(node);
    StringWriter stringWriter = new StringWriter();
    Result result = new StreamResult(stringWriter);
    try {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        if (omitXmlDecl)
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.transform(source, result);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return stringWriter.getBuffer().toString();
}

From source file:Main.java

public static String toString(final Document document) {
    try {//from w  ww.  ja  v a 2  s  .c  o  m
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();

        final TransformerFactory tf = TransformerFactory.newInstance();
        final Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
        //http://johnsonsolutions.blogspot.ca/2007/08/xml-transformer-indent-doesnt-work-with.html
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.transform(new DOMSource(document), new StreamResult(baos));

        final String result = baos.toString();
        return result;
    } catch (final TransformerException e) {
        throw new Error(e);
    }
}

From source file:Main.java

public static void writeXml(Document document, File outxml) {
    try {//from   w ww  .  ja v a 2s .c  o m
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer serializer;
        serializer = tf.newTransformer();
        serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        DOMSource domSource = new DOMSource(document);
        StreamResult streamResult = new StreamResult(outxml);
        serializer.transform(domSource, streamResult);
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException(e);
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static void writeXml(Document document, OutputStream out) {
    try {/* w  w w . ja  v a 2  s. c o m*/
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer serializer;
        serializer = tf.newTransformer();
        serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        DOMSource domSource = new DOMSource(document);
        StreamResult streamResult = new StreamResult(out);
        serializer.transform(domSource, streamResult);
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException(e);
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String transformXmlToString(Document importPackageDocument)
        throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException,
        IOException {/*from  w w  w  .  j av a  2  s  . c  om*/
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    StringWriter writer = new StringWriter();
    javax.xml.transform.Result result = new StreamResult(writer);
    Source source = new DOMSource(importPackageDocument);
    transformer.transform(source, result);
    writer.close();
    String xml = writer.toString();
    return xml;
}

From source file:Main.java

public static void serialize(Document doc, OutputStream out) throws Exception {

    TransformerFactory tfactory = TransformerFactory.newInstance();
    Transformer serializer;//w w  w  .jav  a2s.co  m
    try {
        serializer = tfactory.newTransformer();
        //Setup indenting to "pretty print"
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        serializer.transform(new DOMSource(doc), new StreamResult(out));
    } catch (TransformerException e) {
        // this is fatal, just dump the stack and throw a runtime exception
        e.printStackTrace();

        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static OutputStream writeDocument(Document doc, OutputStream os) {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer;//  w  w  w .j a  v  a2 s . co  m
    try {
        transformer = tFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(os);
        transformer.transform(source, result);
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return os;
}

From source file:Main.java

public static void nodeToString(Node node, StringBuffer buf) throws TransformerException {

    TransformerFactory transFactory = TransformerFactory.newInstance();
    Transformer transformer = transFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

    DOMSource dSource = new DOMSource(node);
    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);
    transformer.transform(dSource, sr);/*from   w ww .  j  ava 2s . co m*/
    StringWriter anotherSW = (StringWriter) sr.getWriter();

    buf.append(anotherSW.getBuffer());

}

From source file:Main.java

/**
 * Uses a TransformerFactory with an identity transformation to convert a
 * Document into a String representation of the XML.
 *
 * @param document Document.// www . j av a2  s. c  om
 * @return An XML String.
 * @throws IOException if an error occurs during transformation.
 */
public static String documentToString(Document document) throws IOException {
    String xml = null;

    try {
        DOMSource dom = new DOMSource(document);
        StringWriter writer = new StringWriter();
        StreamResult output = new StreamResult(writer);

        // Use Transformer to serialize a DOM
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();

        // No need for pretty printing
        transformer.setOutputProperty(OutputKeys.INDENT, INDENT_XML);

        // XML Declarations unexpected whitespace for legacy AS XMLDocument type,
        // so we always omit it. We can't tell whether one was present when
        // constructing the Document in the first place anyway...
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, OMIT_XML_DECLARATION);

        transformer.transform(dom, output);

        xml = writer.toString();
    } catch (TransformerException te) {
        throw new IOException("Error serializing Document as String: " + te.getMessageAndLocation());
    }
    return xml;
}