Java Utililty Methods XML Document Transform

List of utility methods to do XML Document Transform

Description

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

Method

voidtransformDocument(Document doc, String xsl, String targetFile)
transform Document
try {
    Source source = new DOMSource(doc);
    File file = new File(targetFile);
    Result result = new StreamResult(file);
    TransformerFactory factoryT = TransformerFactory.newInstance();
    StreamSource stylesource = new StreamSource(xsl);
    Transformer transformer;
    transformer = factoryT.newTransformer(stylesource);
...
DocumenttransformDocument(Document xmlDocument, String xsltFilename)
Applies a stylesheet to a given xml document.
return transformDocument(xmlDocument, new Hashtable(), xsltFilename);
StringtransformDocumentAsString(Document xmlDocument, Map parameters, String xsltFilename)
Applies a stylesheet (that receives parameters) to a given xml document.
Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(xsltFilename));
if (parameters != null) {
    for (Map.Entry<String, String> param : parameters.entrySet()) {
        transformer.setParameter(param.getKey(), param.getValue());
StringWriter stringWriter = new StringWriter();
StreamResult streamResult = new StreamResult(stringWriter);
...
voidtransformDomDocument(Transformer transformer, Node node, OutputStream os)
Serializes a DOM Node to an OutputStream using JAXP TrAX.
DOMSource source = new DOMSource(node);
StreamResult result = new StreamResult(os);
transformer.transform(source, result);
StringtransformXmlToString(Document importPackageDocument)
transform Xml To String
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();
...
StringtransformXmlToString(Document payload)
Returns dom document as string.
final TransformerFactory transFactory = TransformerFactory.newInstance();
final Transformer transformer = transFactory.newTransformer();
final StringWriter buffer = new StringWriter();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.transform(new DOMSource(payload), new StreamResult(buffer));
return buffer.toString();
StringtransformXMLtoString(Document xmldoc)
transform a XML file to String
DOMConfiguration domConfig = xmldoc.getDomConfig();
DOMSource domSource = new DOMSource(xmldoc);
TransformerFactory tf = TransformerFactory.newInstance();
tf.setAttribute("indent-number", 4);
Transformer serializer = tf.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.VERSION, "1.0");
...
Documentxslt(InputStream stylesheet, Document input)
xslt
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(new StreamSource(stylesheet));
Document result = newDocument();
DOMResult domResult = new DOMResult(result);
transformer.transform(new DOMSource(input), domResult);
return result;
StringxslTransform(Document xmlDoc, Document xslDoc)
Performs XSL transformation.
DOMSource source = new DOMSource(xmlDoc);
DOMSource xslSrc = new DOMSource(xslDoc);
TransformerFactory xformFactory = TransformerFactory.newInstance();
Transformer transformer = xformFactory.newTransformer(xslSrc);
java.io.StringWriter sw = new java.io.StringWriter();
StreamResult outXml = new StreamResult(sw);
transformer.transform(source, outXml);
return sw.toString();
...