Java中的XML节点到字符串 [英] XML Node to String in Java
问题描述
我遇到了这个Java函数来将XML节点转换为Java String表示:
I came across this piece of Java function to convert an XML node to a Java String representation:
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声明,它必须包含缩进。
It looks straightforward in that it wants the output string doesn't have any XML declaration and it must contain indentation.
但我想知道实际输出应该如何,假设我有一个XML节点:
But I wonder how the actual output should be, suppose I have an XML node:
<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>
我可以假设在应用上述转换后得到的字符串是:
Could I assume the resulting String after applying the above transformation is:
"media type="audio" id="au008093" rights="wbowned" title Bee buzz title /media"
我想自己测试一下,但我不知道如何以这个函数实际需要的方式表示这个XML节点。
I want to test it myself, but I have no idea on how to represent this XML node in the way this function actually wants.
我有点困惑,并提前感谢您的慷慨帮助。
I am bit confused, and thanks in advance for the generous help.
推荐答案
您在DOM树中有XML重新表示。
例如,您已经打开了一个XML文件,并且已经在DOM解析器中传递了它。
结果是内存中的DOM树创建XML。
现在只能通过遍历DOM树来访问XML信息。
如果需要,可以使用DOM的XML信息的String表示形式你使用转换的树。
这是因为无法直接从DO获取String表示M树。
所以如果例如节点
你传入 nodeToString
是根XML文档的元素然后结果是包含原始XML数据的String。
标记仍然存在。即您将拥有有效的XML表示。只有这一次才会在String变量中。
You have an XML respesentation in a DOM tree.
For example you have opened an XML file and you have passed it in the DOM parser.
As a result a DOM tree in memory with your XML is created.
Now you can only access the XML info via traversal of the DOM tree.
If you need though, a String representation of the XML info of the DOM tree you use a transformation.
This happens since it is not possible to get the String representation directly from a DOM tree.
So if for example as Node node
you pass in nodeToString
is the root element of the XML doc then the result is a String containing the original XML data.
The tags will still be there. I.e. you will have a valid XML representation. Only this time will be in a String variable.
例如:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = factory.newDocumentBuilder();
Document xmlDoc = parser.parse(file);//file has the xml
String xml = nodeToString(xmlDoc.getDocumentElement());//pass in the root
//xml has the xml info. E.g no xml declaration. Add it
xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?> + xml;//bad to append this way...
System.out.println("XML is:"+xml);
免责声明:甚至没有尝试编译代码。希望你明白你必须做什么
DISCLAIMER: Did not even attempt to compile code. Hopefully you understand what you have to do
这篇关于Java中的XML节点到字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!