开发者

convert java's xml Element object to text

开发者 https://www.devze.com 2023-03-28 21:30 出处:网络
how to convert org.w3c.dom.Element object to text example: from: 开发者_运维百科Element e= doc.createElement(\"element\");

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

精彩评论

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