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

public static String toString(Document doc) {
    try {//from   w w  w.  j a v a 2 s .c  o m
        StringWriter sw = new StringWriter();
        Source source = new DOMSource(doc);
        Result result = new StreamResult(sw);

        TransformerFactory tf = TransformerFactory.newInstance();
        tf.setAttribute("indent-number", new Integer(4));
        Transformer xformer = tf.newTransformer();
        xformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        xformer.setOutputProperty(OutputKeys.INDENT, "yes");
        xformer.transform(source, result);
        return sw.toString();
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.onespatial.jrc.tns.oml_to_rif.fixture.DomBasedUnitTest.java

/**
 * Run once, before any tests are run.//from   www.  j  av  a 2 s. com
 * 
 * @throws FactoryConfigurationError
 *             if any errors occurred configuring the
 *             {@link DocumentBuilderFactory}
 * @throws ParserConfigurationException
 *             if any errors occurred creating a {@link DocumentBuilder}
 * @throws TransformerConfigurationException
 *             if any errors occurred configuring the
 *             {@link TransformerFactory}
 */
@BeforeClass
public static void beforeAnyTest()
        throws ParserConfigurationException, FactoryConfigurationError, TransformerConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    builder = factory.newDocumentBuilder();

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

From source file:com.sdl.odata.renderer.util.PrettyPrinter.java

/**
 * Pretty-print a given XML.//from  w w w .j  av a2 s.  c om
 *
 * @param xml The not-formatted XML.
 * @return The pretty-printed XML.
 */
public static String prettyPrintXml(String xml) throws TransformerException, IOException {

    Source xmlInput = new StreamSource(new StringReader(xml));
    try (StringWriter stringWriter = new StringWriter()) {
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", DEFAULT_INDENT);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.name());
        transformer.setOutputProperty(OutputKeys.VERSION, "1.0");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(xmlInput, xmlOutput);

        return xmlOutput.getWriter().toString();
    }
}

From source file:Main.java

private static String getIndented(Document aDoc) {
    String outputXml = "";
    try {/*w w  w. j  a  va2 s. c  o m*/
        TransformerFactory tf = TransformerFactory.newInstance();
        //      tf.setAttribute("indent-number", new Integer(4));
        Transformer transformer;
        transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(aDoc);
        transformer.transform(source, result);
        outputXml = result.getWriter().toString();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return outputXml;
}

From source file:eu.eidas.engine.test.simple.SSETestUtils.java

/**
 * Prints the tree DOM.//w w  w .  ja v  a  2  s .co  m
 *
 * @param samlToken the SAML token
 * @param isIndent the is indent
 *
 * @return the string
 * @throws TransformerException the exception
 */
public static String printTreeDOM(final Element samlToken, final boolean isIndent) throws TransformerException {
    // set up a transformer
    final TransformerFactory transfac = TransformerFactory.newInstance();
    final Transformer trans = transfac.newTransformer();
    trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    trans.setOutputProperty(OutputKeys.INDENT, String.valueOf(isIndent));

    // create string from XML tree
    final StringWriter stringWriter = new StringWriter();
    final StreamResult result = new StreamResult(stringWriter);
    final DOMSource source = new DOMSource(samlToken);
    trans.transform(source, result);
    final String xmlString = stringWriter.toString();

    return xmlString;
}

From source file:io.selendroid.server.inspector.TreeUtil.java

public static String getXMLSource(JSONObject source) {
    Document document = JsonXmlUtil.buildXmlDocument(source);

    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {/* ww  w .j a  va  2s .  c o  m*/
        transformer = tFactory.newTransformer();
    } catch (TransformerConfigurationException e) {
        throw new SelendroidException(e);
    }

    transformer.setParameter("encoding", "UTF-8");

    DOMSource domSource = new DOMSource(document);
    Writer outWriter = new StringWriter();

    StreamResult result = new StreamResult(outWriter);
    try {
        transformer.transform(domSource, result);
    } catch (TransformerException e) {
        throw new SelendroidException(e);
    }

    return outWriter.toString();
}

From source file:Main.java

/**
 * Writes an element out to a file./*from  w ww .j ava  2s  .c o  m*/
 * @param element The XML element to write out.
 * @param fileName The file name to write to. Existing file is overwriten.
 */
public static void writeElement(Element element, String fileName) {
    File file = new File(fileName);
    file.delete();
    DOMSource domSource = new DOMSource(element);
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(file);
        StreamResult result = new StreamResult(fos);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.transform(domSource, result);
        fos.flush();
        fos.close();
    } catch (Exception e) {
        throw new RuntimeException("Failed to write XML element to:" + fileName, e);
    } finally {
        try {
            fos.flush();
        } catch (Exception e) {
        }
        try {
            fos.close();
        } catch (Exception e) {
        }
    }
}

From source file:ee.ria.xroad.common.Request.java

/**
 * Converts the given SOAP message string to a pretty-printed format.
 * @param soap the SOAP XML to convert/*from www.ja v a 2 s .  c om*/
 * @return pretty-printed String of the SOAP XML
 */
public static String prettyFormat(String soap) {
    try {
        Source xmlInput = new StreamSource(new StringReader(soap));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();

        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * method used to convert a xml document to a string
 * //from w w  w  .  j  a v  a 2 s  . c o m
 * @param doc
 * @return
 */
public static String convertDocumentToString(Document doc) {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer;

    try {
        /**
         * transformation of document happens here
         */
        transformer = tf.newTransformer();
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
        String output = writer.getBuffer().toString();

        /**
         * logging success
         */
        logger.info("xml conversion to string  successful Class:XMLParserUtility line# 98");

        return output;

    } catch (TransformerException e) {
        e.printStackTrace();
        logger.fatal("Could not transform document to XML", e);
    }
    return null;
}

From source file:ac.uk.diamond.sample.HttpClientTest.Utils.java

static String xmlToString(Element doc) {
    StringWriter sw = new StringWriter();
    try {// w  w  w  .j av a 2s  .c o m
        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(sw));
        return sw.toString();
    } catch (TransformerException e) {
        LOG.error("Unable to print message contents: ", e);
        return "<ERROR: " + e.getMessage() + ">";
    }
}