【发布时间】:2011-07-20 12:17:37
【问题描述】:
我有一个 org.w3c.dom.Document。 生成xml是没有问题的。 但是如何从文档中生成 json 呢?
这是获取xml字符串的代码
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
// create string from xml tree
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(document);
trans.transform(source, result);
String xmlString = sw.toString();
【问题讨论】: