开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜