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

voidelementToStream(Element element, OutputStream out)
Write the content of a DOM element to the provided outputstream
DOMSource source = new DOMSource(element);
StreamResult result = new StreamResult(out);
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
transformer.transform(source, result);
voidelementToStream(Element element, OutputStream out)
element To Stream
DOMSource source = new DOMSource(element);
StreamResult result = new StreamResult(out);
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
transformer.transform(source, result);
StringelementToString(Element e)
element To String
StringBuffer buf = new StringBuffer();
buf.append(e.getTagName()).append("{");
for (int i = 0; i < e.getAttributes().getLength(); i++) {
    if (i > 0) {
        buf.append(", ");
    Node n = e.getAttributes().item(i);
    buf.append(attributeName((Attr) n)).append("=").append(n.getNodeValue());
...
StringelementToString(Element el)
element To String
if (el == null)
    return "";
Document document = el.getOwnerDocument();
DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
LSSerializer serializer = domImplLS.createLSSerializer();
return serializer.writeToString(el);
StringelementToString(Element element)
element To String
DOMImplementationLS lsImpl = (DOMImplementationLS) element.getOwnerDocument().getImplementation()
        .getFeature("LS", "3.0");
LSSerializer serializer = lsImpl.createLSSerializer();
serializer.getDomConfig().setParameter("xml-declaration", false); 
return serializer.writeToString(element);
StringelementToString(Element element)
element To String
Document document = element.getOwnerDocument();
DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
LSSerializer serializer = domImplLS.createLSSerializer();
String result = serializer.writeToString(element);
return result;
StringelementToString(Element element)

Converts an Element object into String representation

DOMSource domSource = new DOMSource(element);
StringWriter stringWriter = new StringWriter();
StreamResult streamResult = new StreamResult(stringWriter);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(domSource, streamResult);
return stringWriter.toString();
...
StringelementToString(Element elt, String prefix)
element To String
String str = prefix + "<" + elt.getNodeName();
return str;
StringelementToString(final Element element)
Converts an Element into its XML String representation.
try {
    final DOMSource domSource = new DOMSource(element);
    final StringWriter writer = new StringWriter();
    final StreamResult result = new StreamResult(writer);
    final TransformerFactory tf = TransformerFactory.newInstance();
    final Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
...
StringextractText(Element element)
extract Text
NodeList children = element.getChildNodes();
int length = children.getLength();
for (int n = 0; n < length; ++n) {
    Node next = children.item(n);
    if (next.getNodeType() == Node.TEXT_NODE)
        return next.getNodeValue().trim();
return null;
...