Example usage for javax.xml.transform Transformer transform

List of usage examples for javax.xml.transform Transformer transform

Introduction

In this page you can find the example usage for javax.xml.transform Transformer transform.

Prototype

public abstract void transform(Source xmlSource, Result outputTarget) throws TransformerException;

Source Link

Document

Transform the XML Source to a Result.

Usage

From source file:Main.java

public static void transformDocument(final Document doc, final String xsl, final String targetFile) {
    try {//w  ww .  j  a  v a 2  s. c om
        final Source source = new DOMSource(doc);

        // Creation of the output file
        final File file = new File(targetFile);
        final Result result = new StreamResult(file);

        // configuration of the transformer
        final TransformerFactory factoryT = TransformerFactory.newInstance();
        final StreamSource stylesource = new StreamSource(xsl);
        Transformer transformer;

        transformer = factoryT.newTransformer(stylesource);
        transformer.setOutputProperty(OutputKeys.METHOD, "text");

        // Transformation
        transformer.transform(source, result);
    } catch (final TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (final TransformerException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static String prettyPrintWithTrAX(Document document) throws TransformerException {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    StringWriter stringWriter = new StringWriter();
    StreamResult streamResult = new StreamResult(stringWriter);
    DOMSource domSource = new DOMSource(document);
    transformer.transform(domSource, streamResult);
    return stringWriter.toString();
}

From source file:Main.java

/**
 * @param sourceXML the XML file to transform
 * @param xslt the XSL stylesheet to use for transformation
 * @param targetXML the result of XSL transformation
 * @throws TransformerException if an error occurs configuraing or applying the transformation
 *//*from  w  w w.  j  a v a2s  .c  o m*/
public static void transform(final File sourceXML, final File xslt, final File targetXML)
        throws TransformerException {
    //      logger.debug("sourceXML = " + sourceXML);
    //      logger.debug("xslt = " + xslt);
    //      logger.debug("targetXML = " + targetXML);
    Transformer transformer = createTransformer(xslt, Collections.<String, String>emptyMap());
    Source source = new StreamSource(sourceXML);
    Result result = new StreamResult(targetXML);
    transformer.transform(source, result);
}

From source file:Main.java

public static void transformDocument(Document doc, String xsl, String targetFile) {
    try {//from w  ww  .  j ava 2 s  . c  o  m
        Source source = new DOMSource(doc);

        // Creation of the output file
        File file = new File(targetFile);
        Result result = new StreamResult(file);

        // configuration of the transformer
        TransformerFactory factoryT = TransformerFactory.newInstance();
        StreamSource stylesource = new StreamSource(xsl);
        Transformer transformer;

        transformer = factoryT.newTransformer(stylesource);
        transformer.setOutputProperty(OutputKeys.METHOD, "text");

        // Transformation
        transformer.transform(source, result);
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static String printNode(Node node) {
    String result = "";
    try {/*  w w w .  j a  v a  2s.  com*/
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty("method", "xml");
        StringWriter sw = new StringWriter();
        DOMSource source = new DOMSource(node);
        transformer.transform(source, new StreamResult(sw));
        result = sw.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static String elementToString(Element ele) {
    try {/*  w  w w.  j a v  a  2  s .c  o 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

public static byte[] xmlToBytes(final Node body) throws TransformerException {
    final Transformer transformer = FACTORY.newTransformer();
    final Source source = new DOMSource(body);
    final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    final Result result = new StreamResult(buffer);
    transformer.transform(source, result);
    return buffer.toByteArray();
}

From source file:Main.java

/**
 * Converts a document object to an xml string
 * @param document the document to convert
 * @param documentTransformer the DOM document transformer
 * @return the xml string//from www .jav  a  2s.c  o  m
 */
public static String documentToString(Document document, Transformer documentTransformer)
        throws TransformerException {
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    DOMSource source = new DOMSource(document);
    documentTransformer.transform(source, result);
    return sw.toString();
}

From source file:Main.java

private static String getXmlString(Document document, String tagName)
        throws ParserConfigurationException, SAXException, IOException, TransformerException {
    NodeList nodeList = document.getElementsByTagName(tagName);
    if (nodeList.getLength() < 1) {
        throw new IllegalArgumentException("no " + tagName + " found");
    }//  w ww  .j a  v  a  2  s  .co  m
    Node sub = nodeList.item(0);
    DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document subdoc = db.newDocument();
    subdoc.appendChild(sub.cloneNode(true));

    TransformerFactory transFactory = TransformerFactory.newInstance();
    Transformer transformer = transFactory.newTransformer();
    DOMSource domSource = new DOMSource(subdoc);
    StringWriter sw = new StringWriter();
    StreamResult xmlResult = new StreamResult(sw);
    transformer.transform(domSource, xmlResult);
    sw.flush();
    return sw.toString();
}

From source file:Main.java

public synchronized static void writeXml(Document document, Writer writer, String encoding)
        throws TransformerFactoryConfigurationError, TransformerException {
    if (document != null && writer != null) {
        Source source = new DOMSource(document);
        Result result = new StreamResult(writer);
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.setOutputProperty(OutputKeys.ENCODING,
                (encoding == null || encoding.isEmpty()) ? "UTF-8" : encoding);
        xformer.transform(source, result);
    }/*from   w  w w  .  j a  v a  2 s.c  o m*/
}