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

StringtoString(Node node, boolean formatted)
to String
ByteArrayOutputStream baos = new ByteArrayOutputStream();
writeNode(node, baos, formatted);
return (baos.toString());
StringtoString(Node node, int level, boolean indent)
to String
StringBuilder nodeBuilder = new StringBuilder();
switch (node.getNodeType()) {
case Node.TEXT_NODE:
    if (indent) {
        nodeBuilder.append(setIndent(level));
    nodeBuilder.append(node.getNodeValue());
    if (indent) {
...
StringtoString(Node node, Map outputProperties)
Converts an XML node to a string.
try {
    StringWriter writer = new StringWriter();
    toWriter(node, writer, outputProperties);
    return writer.toString();
} catch (TransformerException e) {
    throw new RuntimeException(e);
StringtoString(Node node, Map outputProperties)
Converts an XML node to a string.
try {
    StringWriter writer = new StringWriter();
    toWriter(node, writer, outputProperties);
    return writer.toString();
} catch (TransformerException e) {
    throw new RuntimeException(e);
voidtoString(Node node, StringBuilder sb)
to String
sb.append(node.getClass().getSimpleName());
if (node instanceof Element) {
    Element el = (Element) node;
    sb.append(":");
    sb.append(el.getNodeName());
    sb.append(" ");
    attrToString(el, sb);
StringtoString(Node xml)
Transform this Node into a String representation.
short type = xml.getNodeType();
if (type == Node.TEXT_NODE)
    return xml.getNodeValue();
StringWriter buffer = new StringWriter();
try {
    TransformerFactory transFactory = TransformerFactory.newInstance();
    Transformer transformer = transFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
...
StringtoString(NodeList nodes)
to String
try {
    StringWriter stw = new StringWriter();
    Transformer serializer = TransformerFactory.newInstance().newTransformer();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node item = nodes.item(i);
        switch (item.getNodeType()) {
        case Node.TEXT_NODE:
            stw.append(item.getTextContent());
...
StringtoStringE(Node element)
to String E
try {
    if (element == null) {
        return "null";
    Source source = new DOMSource(element);
    StringWriter stringWriter = new StringWriter();
    try (PrintWriter printWriter = new PrintWriter(stringWriter)) {
        Result result = new StreamResult(printWriter);
...
StringtoText(Node node)
This class converts a DOM representation node to text.
String ret = null;
try {
    ByteArrayOutputStream xmlstr = new ByteArrayOutputStream();
    DOMSource source = new DOMSource(node);
    StreamResult result = new StreamResult(xmlstr);
    Transformer trans = TransformerFactory.newInstance().newTransformer();
    trans.transform(source, result);
    xmlstr.close();
...
StringtoText(Node node)
to Text
try {
    DOMSource source = new DOMSource(node);
    Transformer identity = TransformerFactory.newInstance().newTransformer();
    identity.setOutputProperty(OutputKeys.INDENT, "no");
    identity.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Result result = new StreamResult(baos);
    identity.transform(source, result);
...