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(String xmlsource, File xsltfile)
transform
Source xmlSource = new StreamSource(new StringReader(xmlsource));
Source xsltSource = new StreamSource(xsltfile);
StringWriter stringWriter = new StringWriter();
TransformerFactory transFact = TransformerFactory.newInstance();
Templates cachedXSLT = transFact.newTemplates(xsltSource);
Transformer trans = cachedXSLT.newTransformer();
trans.transform(xmlSource, new StreamResult(stringWriter));
return stringWriter.toString();
...
Nodetransform(String xslSource, Node original)
transform
StringReader sr = new StringReader(xslSource);
DOMResult result = new DOMResult();
doTransform(new StreamSource(sr), new DOMSource(original), result);
return result.getNode();
Stringtransform(Transformer transformer, Node element)
transform
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(element);
transformer.transform(source, result);
String xmlString = result.getWriter().toString();
return xmlString;
Stringtransformar(String xmlOrigen, String xslOrigen)
transformar
InputStream is = new ByteArrayInputStream(xmlOrigen.getBytes("UTF-8"));
Source xmlSource = new StreamSource(is);
Source xsltSource = new StreamSource(new File(xslOrigen));
StringWriter cadenaSalida = new StringWriter();
Result bufferResultado = new StreamResult(cadenaSalida);
TransformerFactory factoriaTrans = TransformerFactory.newInstance();
Transformer transformador = factoriaTrans.newTransformer(xsltSource);
transformador.transform(xmlSource, bufferResultado);
...
Transformertransformer()
transformer
TransformerFactory tf = TRANSFORMER_FACTORY.get();
if (tf == null) {
    tf = TransformerFactory.newInstance();
    TRANSFORMER_FACTORY.set(tf);
return tf.newTransformer();
voidtransformerExceptionMsg(TransformerException ex, Node contextNode, String str)
transformer Exception Msg
System.out.println("[XPathUtil] Transformer exception selecting " + "XObject for " + "XPath String " + str
        + " with context node " + nodeToString(contextNode) + ": " + ex);
TransformerFactorytransformerFactory()
a bit of boilerplate
final TransformerFactory tf = TransformerFactory.newInstance();
return tf;
voidtransformFile(Source stylesheetFile, File input, File output)
transform File
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(stylesheetFile);
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(new StreamSource(input), new StreamResult(output));
StringtransformLogViaXSLT(String logFilePath, String xslFilePath)
transform Log Via XSLT
ByteArrayOutputStream baos = new ByteArrayOutputStream();
File logFile = new File(logFilePath);
if (logFile.exists()) {
    File xslFile = new File(xslFilePath);
    if (xslFile.exists()) {
        TransformerFactory factory = TransformerFactory.newInstance();
        Source xslt = new StreamSource(new File(xslFilePath));
        Transformer transformer = factory.newTransformer(xslt);
...
voidtransformNonTextNodeToOutputStream(Node node, OutputStream os, boolean omitXmlDeclaration)
transform Non Text Node To Output Stream
TransformerFactory tf = TransformerFactory.newInstance();
Transformer trans = tf.newTransformer();
if (omitXmlDeclaration) {
    trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
trans.transform(new DOMSource(node), new StreamResult(os));