Java Utililty Methods XML Node Transform

List of utility methods to do XML Node Transform

Description

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

Method

StringbuildXml(Node node)
build Xml
StringWriter writer = new StringWriter();
buildXml(node, writer);
return writer.toString();
booleancopyTreeToResult(Node tree, Result result)
Copy DOM tree to a SOAP tree.
try {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    DOMSource source = new DOMSource(tree);
    transformer.transform(source, result);
    return true; 
} catch (TransformerConfigurationException tce) {
    tce.printStackTrace();
...
InputStreamcreateInputStream(Node node)
create Input Stream
ByteArrayOutputStream stream = new ByteArrayOutputStream();
TransformerFactory.newInstance().newTransformer().transform(new DOMSource(node), new StreamResult(stream));
return new ByteArrayInputStream(stream.toByteArray());
StringcreateXML(Node doc)
create XML
TransformerFactory xff = TransformerFactory.newInstance();
Transformer serializer = null;
try {
    serializer = xff.newTransformer();
} catch (TransformerConfigurationException te) {
    throw new RuntimeException("Could not create transformer. " + te.getMessage());
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
...
StringgetAsText(Node n)
get As Text
try {
    Transformer t = TransformerFactory.newInstance().newTransformer();
    StringWriter out = new StringWriter();
    t.transform(new javax.xml.transform.dom.DOMSource(n), new javax.xml.transform.stream.StreamResult(out));
    return out.toString();
} catch (Throwable t) {
    t.printStackTrace();
    return null;
...
StringgetHumanReadableXml(final Node node)
Converts the provided DOM Node to a pretty-printed XML-formatted string.
StringWriter toReturn = new StringWriter();
try {
    Transformer transformer = getFactory().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
    transformer.transform(new DOMSource(node), new StreamResult(toReturn));
} catch (TransformerException e) {
    throw new IllegalStateException("Could not transform node [" + node.getNodeName() + "] to XML", e);
...
StringgetNodeValue(NodeList nodeList)
get Node Value
final Transformer serializer = TransformerFactory.newInstance().newTransformer();
serializer.setURIResolver(null);
final StringBuilder buf = new StringBuilder();
for (int i = 0; i < nodeList.getLength(); i++) {
    final StringWriter sw = new StringWriter();
    serializer.transform(new DOMSource(nodeList.item(i)), new StreamResult(sw));
    String xml = sw.toString();
    final Matcher matcher = HEADER_PATTERN.matcher(xml);
...
StringgetValueOfValueNode(Node n, boolean unescape)
Method to get the value of "Value" node If unescape is set to false, xml escaped chars will not be unescaped.
return getValueOfValueNodeNoTrim(n, unescape).trim();
StringgetXML(NodeList childNodes)
get XML
try {
    StringBuilder builder = new StringBuilder();
    Transformer t = TransformerFactory.newInstance().newTransformer();
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    for (int i = 0; i < childNodes.getLength(); i++) {
        StringWriter sw = new StringWriter();
        t.transform(new DOMSource(childNodes.item(i)), new StreamResult(sw));
        builder.append(sw.toString());
...
byte[]nodeToByteArray(Node node)
Returns a byte array from given node.
Source source = new DOMSource(node);
ByteArrayOutputStream out = new ByteArrayOutputStream();
Result result = new StreamResult(out);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.transform(source, result);
return out.toByteArray();