Java Utililty Methods XML Element to String

List of utility methods to do XML Element to String

Description

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

Method

StringgetStringValue(Element from)
get String Value
String value = from.getFirstChild().getNodeValue();
return value;
StringgetStringValue(final Element element)
Get element node string value.
final StringBuilder buf = new StringBuilder();
final NodeList children = element.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
    final Node n = children.item(i);
    switch (n.getNodeType()) {
    case Node.TEXT_NODE:
        buf.append(n.getNodeValue());
        break;
...
StringgetStringValueByTagName(Element element, String tagName)
get String Value By Tag Name
NodeList nl1 = element.getElementsByTagName(tagName);
if (nl1.getLength() <= 0) {
    return null;
Element el = (Element) nl1.item(0);
return el.getChildNodes().item(0).getNodeValue();
StringgetStringValueOfElement(Element elem)
Exactly like value-of()
StringBuffer buf = new StringBuffer();
NodeList nodes = elem.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
    Node node = nodes.item(i);
    switch (node.getNodeType()) {
    case Node.TEXT_NODE:
        buf.append(((Text) node).getData());
        break;
...
ListgetStringValues(Element from, String elementName)
get String Values
List<String> values = new LinkedList<String>();
NodeList nodes = from.getElementsByTagName(elementName);
if (nodes != null) {
    for (int i = 0; i < nodes.getLength(); i++) {
        values.add(nodes.item(i).getChildNodes().item(0).getNodeValue());
return values;
...
StringgetXmlString(Element node)
get Xml String
return toString(node);
StringgetXMLStringFromNode(Element node)
get XML String From Node
DOMSource domSource = new DOMSource(node);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
return result.getWriter().toString();
StringtoString(Element e)
to String
try {
    TransformerFactory tfactory = TransformerFactory.newInstance();
    Transformer xform = tfactory.newTransformer();
    Source src = new DOMSource(e);
    java.io.StringWriter writer = new StringWriter();
    Result result = new javax.xml.transform.stream.StreamResult(writer);
    xform.transform(src, result);
    return writer.toString();
...
StringtoString(Element element)
Transform an Element into a String.
try {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    Source source = new DOMSource(element);
    Result target = new StreamResult(out);
    transformer.transform(source, target);
    return out.toString("UTF-8");
...
StringtoString(Element node)
to String
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
StringWriter buffer = new StringWriter();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(node), new StreamResult(buffer));
return buffer.toString();