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 byte[] documentToByteArray(Document data, Integer indent) throws TransformerException {
    ByteArrayOutputStream result = new ByteArrayOutputStream();

    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    if (indent != null) {
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", indent.toString());
    }/*w  ww .j  a  va 2 s. c om*/
    transformer.transform(new DOMSource(data), new StreamResult(result));
    return result.toByteArray();
}

From source file:Main.java

public static void marshalToStream(Document doc, OutputStream ostream, boolean indent) throws Exception {
    TransformerFactory transFac = TransformerFactory.newInstance();
    Transformer trans = transFac.newTransformer();
    if (indent) {
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    }//from   w w  w  . j  a  v  a 2 s.  co  m
    trans.setOutputProperty(OutputKeys.STANDALONE, "no");

    trans.transform(new DOMSource(doc), new StreamResult(ostream));
}

From source file:Main.java

public static void marshalToStream(Element elm, OutputStream ostream, boolean indent) throws Exception {
    TransformerFactory transFac = TransformerFactory.newInstance();
    Transformer trans = transFac.newTransformer();
    if (indent) {
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    }/*w ww . ja v a 2s .  co  m*/
    trans.setOutputProperty(OutputKeys.STANDALONE, "no");

    trans.transform(new DOMSource(elm), new StreamResult(ostream));
}

From source file:com.iflytek.spider.util.DomUtil.java

/**
 * save dom into ouputstream//ww w.  ja va2  s .co  m
 * 
 * @param os
 * @param e
 */
public static void saveDom(OutputStream os, Element e) {

    DOMSource source = new DOMSource(e);
    TransformerFactory transFactory = TransformerFactory.newInstance();
    Transformer transformer;
    try {
        transformer = transFactory.newTransformer();
        transformer.setOutputProperty("indent", "yes");
        StreamResult result = new StreamResult(os);
        transformer.transform(source, result);
        os.flush();
    } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace(LogUtil.getWarnStream(LOG));
    } catch (IOException e1) {
        e1.printStackTrace(LogUtil.getWarnStream(LOG));
    } catch (TransformerConfigurationException e2) {
        e2.printStackTrace(LogUtil.getWarnStream(LOG));
    } catch (TransformerException ex) {
        ex.printStackTrace(LogUtil.getWarnStream(LOG));
    }
}

From source file:Main.java

public static String constructXMLString(Document dom) throws IOException {
    String retXMLStr = null;/*from   w w w.  j  a va  2s .  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

/**
 * Writes the XML document to the particular file specified as argument
 * //  w  ww  .  ja  v  a2s  .c o m
 * @param doc the document
 * @param filename the path to the file in which to write the XML data writing
 *            operation fails
 */
public static void writeXMLDocument(Document doc, String filename) {
    try {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer;
        transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(filename));
        transformer.transform(source, result);
        log.fine("writing operation to " + filename + " successful!");
    } catch (TransformerConfigurationException e) {
        log.warning(e.getMessage());
    } catch (TransformerException e) {
        log.warning(e.getMessage());
    }
}

From source file:Main.java

/**
 * @param dom//from w  w w.  ja va 2  s  . c  om
 * @param outFile
 */
public static void writeDomToFile(Document dom, File outFile) {
    try {
        TransformerFactory transFact = TransformerFactory.newInstance();
        Transformer transformer = transFact.newTransformer();
        DOMSource source = new DOMSource(dom);
        StreamResult result;

        result = new StreamResult(
                new OutputStreamWriter(new FileOutputStream(outFile), System.getProperty("file.encoding")));
        transformer.transform(source, result);
    } catch (Exception e) {
        throw new RuntimeException(
                "Could not write dom tree '" + dom.getBaseURI() + "' to file '" + outFile.getName() + "'!", e);
    }
}

From source file:Main.java

public static boolean transform(InputStream xsltInputStream, InputStream xmlInputStream,
        OutputStream outputStream, Map<String, Object> parameterMap) {

    boolean result = false;

    try {/*from  ww  w  .j ava2  s.  c  o m*/

        Source source = new StreamSource(xsltInputStream);
        Transformer transformer = TransformerFactory.newInstance().newTemplates(source).newTransformer();

        for (Entry<String, Object> entry : parameterMap.entrySet()) {
            transformer.setParameter(entry.getKey(), entry.getValue());
        }

        transformer.transform(new StreamSource(xmlInputStream), new StreamResult(outputStream));

        result = true;

    } catch (TransformerException | TransformerFactoryConfigurationError e) {
    }

    return result;
}

From source file:Main.java

/**
 * Returns an XML representation of the provided node.
 *
 * @param node the node to be represented in XML.
 *
 * @return a string containing an XML representation of the
 * provided DOM node.//from   ww w .j a v  a 2 s.c o  m
 */
public static String nodeToXmlString(Node node) {

    try {

        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.INDENT, "yes");

        DOMSource source = new DOMSource(node);
        StreamResult result = new StreamResult(new StringWriter());

        t.transform(source, result);

        return result.getWriter().toString();

    } catch (TransformerException e) {
        throw new IllegalStateException(e);
    } catch (TransformerFactoryConfigurationError e) {
        throw new IllegalStateException(e);
    }

}

From source file:Main.java

/**
 * Pretty print an XML. Use with caution as this drains the source if it is
 * a StreamSource./*  ww w.  j a  va  2s .  c o  m*/
 * @param source the XML source
 * @return a String with readable XML
 */
public static String prettyPrint(final Source source) {
    try {
        Transformer serializer = TransformerFactory.newInstance().newTransformer();
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        StringWriter writer = new StringWriter();
        serializer.transform(source, new StreamResult(writer));
        return writer.toString();
    } catch (TransformerException e) {
        return e.getMessage();
    }
}