Java Utililty Methods XML Transform

List of utility methods to do XML Transform

Description

The list of methods to do XML Transform are organized into topic(s).

Method

Stringtransform(File xslt_source, File xml_source)
Transforms the XML using an XSLT.
return transform(new StreamSource(xslt_source), new StreamSource(xml_source));
voidtransform(final File sourceXML, final File xslt, final File targetXML)
transform
Transformer transformer = createTransformer(xslt, Collections.<String, String>emptyMap());
Source source = new StreamSource(sourceXML);
Result result = new StreamResult(targetXML);
transformer.transform(source, result);
Stringtransform(final Source xml, final Source xslt, final Map params, final Writer err)
transform
TransformerFactory factory = TransformerFactory.newInstance();
factory.setErrorListener(new ErrorListener() {
    public void warning(final TransformerException e) throws TransformerException {
        append(e.getMessageAndLocation());
    public void error(final TransformerException e) throws TransformerException {
        append(e.getMessageAndLocation());
    public void fatalError(final TransformerException e) throws TransformerException {
        append(e.getMessageAndLocation());
    private void append(final String msg) {
        if (err != null) {
            try {
                err.append(msg + "\n");
            } catch (IOException e) {
                e.printStackTrace();
});
Transformer transform = factory.newTransformer(xslt);
if (params != null) {
    for (Iterator iter = params.entrySet().iterator(); iter.hasNext();) {
        Entry param = (Entry) iter.next();
        transform.setParameter((String) param.getKey(), param.getValue());
StreamResult result = new StreamResult(new StringWriter());
transform.transform(xml, result);
return result.getWriter().toString();
voidtransform(InputStream styleSheet, InputStream xml, Writer out)
transform
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(new StreamSource(styleSheet));
transformer.transform(new StreamSource(xml), new StreamResult(out));
voidtransform(InputStream styleSheet, InputStream xml, Writer out)
Transform input xml given a stylesheet.
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(new StreamSource(styleSheet));
transformer.transform(new StreamSource(xml), new StreamResult(out));
voidtransform(InputStream xmlStream, InputStream xslStream, OutputStream outputStream)
transform
Source xmlStreamSource = new StreamSource(xmlStream);
Source xslStreamSource = new StreamSource(xslStream);
Result result = new StreamResult(outputStream);
Transformer transformer = TransformerFactory.newInstance().newTransformer(xslStreamSource);
transformer.transform(xmlStreamSource, result);
voidtransform(InputStream xslis, InputStream xmlis, OutputStream xmlos)
transform
try {
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(new StreamSource(xslis));
    transformer.transform(new StreamSource(xmlis), new StreamResult(xmlos));
    xslis.close();
    xmlis.close();
    xmlos.close();
} catch (Exception e) {
...
booleantransform(InputStream xsltInputStream, InputStream xmlInputStream, OutputStream outputStream, Map parameterMap)
transform
boolean result = false;
try {
    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));
...
voidtransform(javax.xml.transform.Source source, javax.xml.transform.Result result, boolean indent)
Executes a transformation.
TransformerFactory factory = TransformerFactory.newInstance();
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
factory.setFeature("http://javax.xml.XMLConstants/feature/secure-processing", true);
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, DEFAULT_ENCODING);
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
if (indent) {
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
...
voidtransform(Reader source, Writer result, String style)
This method will apply an XSLT transformation
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(new StreamSource(new StringReader(style)));
transformer.transform(new StreamSource(source), new StreamResult(result));