我发现了一个将XML节点转换为Java字符串表示的Java函数:
private String nodeToString(Node node) {
StringWriter sw = new StringWriter();
try {
Transformer t = TransformerFactory.newInstance().newTransformer();
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.transform(new DOMSource(node), new StreamResult(sw));
} catch (TransformerException te) {
System.out.println("nodeToString Transformer Exception");
}
return sw.toString();
}
看起来很简单,它希望输出的字符串没有任何XML声明,并且必须包含缩进。
但我想知道实际输出应该是什么样的,假设我有一个XML节点:
<p><media type="audio" id="au008093" rights="wbowned">
<title>Bee buzz</title>
</media>Most other kinds of bees live alone instead of in a colony. These bees make
tunnels in wood or in the ground. The queen makes her own nest.</p>
我可以假设应用上述转换后的结果是什么字符串:
"media type="audio" id="au008093" rights="wbowned" title Bee buzz title /media"
我想亲自测试,但我不知道如何以此函数实际希望的方式表示此XML节点。
我有些困惑,在此先感谢您慷慨的帮助。