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

voidtransfer(Document doc, String filepath)
transfer
try {
    Transformer tf = tff.newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(filepath);
    tf.transform(source, result);
} catch (TransformerConfigurationException e) {
    e.printStackTrace();
} catch (TransformerException e) {
...
voidtransform(Document doc, Result result)
transform
try {
    Source source = new DOMSource(doc);
    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.setOutputProperty(OutputKeys.INDENT, "yes");
    xformer.transform(source, result);
} catch (Exception e) {
    e.printStackTrace();
voidtransform(Document doc, Result result)
transform
try {
    Source source = new DOMSource(doc);
    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.setOutputProperty(OutputKeys.INDENT, "yes");
    xformer.transform(source, result);
} catch (TransformerConfigurationException e) {
    e.printStackTrace();
    throw e;
...
Nodetransform(Document doc, StreamSource xslInput, String systemid)
Purpose: XML transformation using XSL
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(xslInput);
DOMResult domResult = new DOMResult();
DOMSource xmlDomSource = null;
xmlDomSource = new DOMSource(doc);
xmlDomSource.setSystemId(systemid);
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
...
voidtransform(Document document, Properties outputProperties, StreamResult streamResult)
Main transformation method.
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
if (outputProperties != null) {
    Enumeration<?> propertyNames = outputProperties.propertyNames();
    while (propertyNames.hasMoreElements()) {
        String propName = propertyNames.nextElement().toString();
        String propValue = outputProperties.getProperty(propName);
        if (propValue == null) {
...
Documenttransform(Document dom, Document xslt)
transform
try {
    DOMSource xsltSource = new DOMSource(xslt);
    Transformer transformer = transFactory.newTransformer(xsltSource);
    DOMSource source = new DOMSource(dom);
    Document out = newDocument();
    DOMResult result = new DOMResult(out);
    transformer.transform(source, result);
    return out;
...
voidtransform(final Document document, final StreamResult streamResult, final boolean xmlDeclaration)
Transform.
if (document == null) {
    throw new IllegalArgumentException("document cannot be NULL!");
if (streamResult == null) {
    throw new IllegalArgumentException("streamResult cannot be NULL!");
Transformer transformer;
try {
...
Stringtransform(Source xsl, Document doc)
transform
StringWriter writer = new StringWriter();
try {
    TransformerFactory.newInstance().newTransformer(xsl).transform(new DOMSource(doc),
            new StreamResult(writer));
} catch (Exception e) {
return writer.toString();
StringtransformDoc(Document doc)
transform Doc
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
return result.getWriter().toString();
FiletransformDocToFile(Document doc, File file)
transform Doc To File
final TransformerFactory tf = TransformerFactory.newInstance();
final Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.transform(new DOMSource(doc), new StreamResult(file));
return file;