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

StringGetOuterXml(Node node)
Gets the outerXML from a Node
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty("omit-xml-declaration", "yes");
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(node), new StreamResult(writer));
return writer.toString();
StringgetStringFromDocument(Node doc)
get String From Document
try {
    DOMSource domSource = new DOMSource(doc);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.transform(domSource, result);
    return writer.toString();
...
StringgetStringFromNode(Node node)
get String From Node
StringWriter writer = new StringWriter();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(node), new StreamResult(writer));
String xml = writer.toString();
return xml;
StringgetStringFromNode(Node node)
Converts a node to a string.
DOMSource domSource = new DOMSource(node);
ByteArrayOutputStream baos = null;
try {
    baos = new ByteArrayOutputStream();
    StreamResult result = new StreamResult(baos);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(domSource, result);
...
StringgetStringFromXML(Node node)
get String From XML
return getStringFromXML(node, null);
StringgetText(final Node node)
get Text
final StringBuilder result = new StringBuilder();
if (!node.hasChildNodes())
    return "";
final NodeList list = node.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
    Node subnode = list.item(i);
    if (subnode.getNodeType() == Node.TEXT_NODE) {
        result.append(subnode.getNodeValue());
...
StringgetXml(Node node)
Returns a string that represents the given node.
try {
    Transformer tf = TransformerFactory.newInstance().newTransformer();
    tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    StreamResult dest = new StreamResult(new StringWriter());
    tf.transform(new DOMSource(node), dest);
    return dest.getWriter().toString();
} catch (Exception e) {
...
StringgetXmlAsString(Node node)
get Xml As String
Transformer transformer = getTransformer();
DOMSource source = new DOMSource(node);
StringWriter xmlString = new StringWriter();
StreamResult streamResult = new StreamResult(xmlString);
transformer.transform(source, streamResult);
return xmlString.toString();
StringgetXMLString(Node doc)
get XML String
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
return writer.toString();
StringgetXmlString(Node n)
get Xml String
Transformer serializer = null;
try {
    serializer = TransformerFactory.newInstance().newTransformer();
    serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter sw = new StringWriter();
    StreamResult out = new StreamResult(sw);
    serializer.transform(new DOMSource(n), out);
    String result = ((StringWriter) out.getWriter()).getBuffer().toString();
...