Reputation: 47
how to convert org.w3c.dom.Element object to text
example:
from:
Element e= doc.createElement("element");
e.setAttribute("x", "10");
need function to transform to :
result text :
<element x="10"/>
or:
<element x="10"></element>
Upvotes: 1
Views: 1328
Reputation: 403581
Using only the standard API, this works:
Element element = ...
StringWriter buffer = new StringWriter();
TransformerFactory.newInstance().newTransformer().transform(
new DOMSource(element), new StreamResult(buffer)
);
String xml = buffer.toString();
Not pretty, but avoids using proprietary APIs.
Upvotes: 1