如何使用 Java 标准 DOM API 解析 DocumentFragment
这就是我如何用 Java 解析格式良好的 XML 文档:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
// text contains the XML content
Document doc = builder.parse(new InputSource(new StringReader(text)));
文本示例如下:
<a>
<b/>
</a>
How can I parse a DocumentFragment?例如,以下内容:
<a>
<b/>
</a>
<a>
<b/>
</a>
注意:如果可能的话,我想使用 org.w3c.dom,而不使用其他库/技术。
This is how I can parse a well-formed XML document in Java:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
// text contains the XML content
Document doc = builder.parse(new InputSource(new StringReader(text)));
An example for text is this:
<a>
<b/>
</a>
How can I parse a DocumentFragment? For example, this:
<a>
<b/>
</a>
<a>
<b/>
</a>
NOTE: I want to use org.w3c.dom
and no other libraries/technologies, if possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我只是想到了一个愚蠢的解决方案。我可以将片段包装在一个虚拟元素中,如下所示:
然后再次以编程方式过滤掉该虚拟元素,如下所示:
但这有点蹩脚,让我们看看是否还有其他解决方案。
I just thought of a silly solution. I could wrap the fragment in a dummy element like this:
And then programmatically filter out that dummy element again, like this:
But that's a bit lame, let's see if there is any other solution.
进一步扩展已经给出的答案:
Further expanding on the answers already given:
我建议不要使用 DOM API。它又慢又丑。
请改用流式 StAX。它内置于 JDK 1.6+ 中。您可以一次获取一个元素,如果缺少根元素,它也不会阻塞。
http://en.wikipedia.org/wiki/StAX
http://download.oracle.com/javase/6/docs/api/javax/xml/stream/XMLStreamReader.html
I would suggest not using the DOM API. It's slow and ugly.
Use streaming StAX instead. It's built into JDK 1.6+. You can fetch one element at a time, and it won't choke if you're missing a root element.
http://en.wikipedia.org/wiki/StAX
http://download.oracle.com/javase/6/docs/api/javax/xml/stream/XMLStreamReader.html