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

StringnodeToString(Node n)
node To String
StringBuilder sb = new StringBuilder();
if (n instanceof Text)
    return n.getNodeValue().trim();
else {
    NodeList nl = n.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node child = nl.item(i);
        sb.append(nodeToString(child));
...
StringnodeToString(Node node)
node To String
if (node == null) {
    return null;
final Transformer transformer;
try {
    transformer = transformerFactory.newTransformer();
} catch (TransformerConfigurationException e) {
    throw new IllegalStateException(String.format("Failed to build %s", Transformer.class.getName()), e);
...
StringnodeToString(Node node)
node To String
String ret = "<null/>";
if (node != null) {
    ret = "<" + node.getNodeName() + " ";
    NamedNodeMap attrs = node.getAttributes();
    if (attrs != null) {
        for (int i = 0; i < attrs.getLength(); i++) {
            Node attr = (Node) attrs.item(i);
            ret = ret + attr.getNodeName() + "='";
...
StringnodeToString(Node node)
node To String
String strNode = "";
Node nodeIt = node;
if (nodeIt instanceof Element) {
    Element elem = (Element) nodeIt;
    String strElem = "";
    strElem += "<" + elem.getTagName();
    NamedNodeMap attribs = elem.getAttributes();
    int len = attribs.getLength();
...
StringnodeToString(Node node)
node To String
try {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(node);
    transformer.transform(source, result);
    return result.getWriter().toString();
} catch (TransformerConfigurationException ex) {
...
StringnodeToString(Node node)
This method serializes a Node into a String
try {
    if (node != null && node.getLocalName().equals("temporary-simple-type-wrapper")) {
        return node.getTextContent();
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    StringWriter stringWriter = new StringWriter();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
...
StringnodeToString(Node node)
node To String
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(node);
trans.transform(source, result);
...
StringnodeToString(Node node)
Converts a Node into a String using the DOM, level 3, Load/Save serializer.
StringWriter writer = new StringWriter();
writeNode(node, writer);
return writer.toString();
StringnodeToString(Node node)
node To String
StringWriter stringWriter = new StringWriter();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(node), new StreamResult(stringWriter));
return stringWriter.toString();
StringnodeToString(Node node)
node To String
Source source = new DOMSource(node);
StringWriter stringWriter = new StringWriter();
Result result = new StreamResult(stringWriter);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(source, result);
return stringWriter.getBuffer().toString();