Java Utililty Methods XML Node to String

List of utility methods to do XML Node to String

Description

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

Method

voiddump(Node node)
dump
try {
    TransformerFactory fac = TransformerFactory.newInstance();
    Transformer t = fac.newTransformer();
    FileOutputStream fos = new FileOutputStream(new File("xsl.xsl"));
    t.transform(new DOMSource(node), new StreamResult(fos));
    fos.close();
} catch (Exception e) {
    e.printStackTrace();
...
voiddumpNode(Node node)
Serializes a W3C DOM node to a String and dumps it to System.out.
try {
    Transformer trans = TransformerFactory.newInstance().newTransformer();
    trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    trans.setOutputProperty(OutputKeys.INDENT, "yes");
    Source src = new DOMSource(node);
    Result res = new StreamResult(System.out);
    trans.transform(src, res);
} catch (Exception e) {
...
StringelementToString(Node element)
element To String
Transformer transformer = TransformerFactory.newInstance().newTransformer();
return transform(transformer, element);
StringgetAttributeValue(Node node, String attrName)
get Attribute Value
String value = null;
node = node.getAttributes().getNamedItem(attrName);
if (node != null) {
    value = node.getNodeValue();
return value;
StringgetDescriptionOfNode(Node node)
get Description Of Node
if (!(node instanceof Element)) {
    return domToString(node);
final String id = ((Element) node).getAttribute("id");
if ((id == null) || (id.length() == 0)) {
    return domToString(node);
return "Node: id=" + id;
...
InputStreamgetNodeAsInputStream(Node node)
get Node As Input Stream
Transformer transformer = TransformerFactory.newInstance().newTransformer();
ByteArrayOutputStream out = new ByteArrayOutputStream();
StreamResult res = new StreamResult(out);
transformer.transform(new DOMSource(node), res);
return new ByteArrayInputStream(out.toByteArray());
InputStreamgetNodeAsStream(Node node)
Stream a DOM Node as an input stream
Source source = new DOMSource(node);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Result streamResult = new StreamResult(baos);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(source, streamResult);
ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
return bis;
...
StringgetNodeSubtreeXMLString(final Node node)
get Node Subtree XML String
final StringWriter writer = new StringWriter();
writeNodeSubtreeXMLString(node, writer);
return writer.toString();
StringgetNodeXml(Node node)
get Node Xml
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer trans = transformerFactory.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StringWriter sw = new StringWriter();
trans.transform(new DOMSource(node), new StreamResult(sw));
return sw.getBuffer().toString();
StringgetOuterXml(Node n)
get Outer Xml
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = null;
try {
    transformer = transFactory.newTransformer();
} catch (TransformerConfigurationException e1) {
    e1.printStackTrace();
    return "";
try {
    StringWriter buffer = new StringWriter();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.transform(new DOMSource(n), new StreamResult(buffer));
    return buffer.toString();
} catch (TransformerConfigurationException e) {
    e.printStackTrace();
    return "";
} catch (TransformerException e) {
    e.printStackTrace();
    return "";