convert java's xml Element object to text
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>
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.
精彩评论