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.
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.