How to parse a DocumentFragment with with the Java standard DOM API
Asked Answered
B

3

7

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.

Betz answered 11/8, 2011 at 13:2 Comment(0)
B
6

I just thought of a silly solution. I could wrap the fragment in a dummy element like this:

<dummy><a>
  <b/>
</a>
<a>
  <b/>
</a></dummy>

And then programmatically filter out that dummy element again, like this:

String wrapped = "<dummy>" + text + "</dummy>";
Document parsed = builder.parse(new InputSource(new StringReader(wrapped)));
DocumentFragment fragment = parsed.createDocumentFragment();

// Here, the document element is the <dummy/> element.
NodeList children = parsed.getDocumentElement().getChildNodes();

// Move dummy's children over to the document fragment
while (children.getLength() > 0) {
    fragment.appendChild(children.item(0));
}

But that's a bit lame, let's see if there is any other solution.

Betz answered 11/8, 2011 at 13:12 Comment(5)
Exactly what I was going to suggest - you beat me to it.Stempson
XML parsers in other platforms support a DocumentFragment so you needn't add a hackHatchett
@Phlip: What are those "other platforms", and how would that have helped me back when I asked / answered this question?Betz
Gnome's libxml2 (which Python and Ruby use) permit fragments. But I admit I'm not helping you so much as trying to help the community...Hatchett
@Phlip: This is a quite Java specific question about "the Java standard DOM API", so I'm not convinced this helps the community...Betz
V
0

Further expanding on the answers already given:

public static DocumentFragment stringToFragment(Document document, String source) throws Exception
{
    source = "<dummy>" + source + "</dummy>";
    Node node = stringToDom(source).getDocumentElement();
    node = document.importNode(node, true);
    DocumentFragment fragment = document.createDocumentFragment();
    NodeList children = node.getChildNodes();
    while (children.getLength() > 0)
    {
        fragment.appendChild(children.item(0));
    }
    return fragment;
}
Venipuncture answered 28/3, 2014 at 18:1 Comment(2)
All you need is a stringToDom() now.Fitzger
I think an answer https://mcmap.net/q/76598/-how-can-i-parse-a-html-string-in-java shows how this can be implementedMcgann
L
-2

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

Labyrinthine answered 11/8, 2011 at 17:41 Comment(3)
Thanks. I don't have a choice but to use DOM, as I'm working on a big legacy system. Generally, it's neither slow nor ugly, IMO... Unless you can prove slowness to me with benchmarks?Betz
I suppose slow is a relative term. DOM is fine for smaller documents. For large ones it consumes too much memory, and that's what slows things down.Labyrinthine
@Labyrinthine A minimal example using StAX (Java 1.7, Xerces as implementation) will show that it will choke to death if the xml is not well formed (missing a root element). Using <herpTag/><derpTag/> will result in an XMLStreamException stating "The markup in the document following the root element must be well-formed". My intention was to use StAX to assemble a DocumentFragment object. Do you have an example of using StAX in this manner? It would be nice to create DocumentFragments without having to implement a parser or wrap things in dummy tags.Cotyledon

© 2022 - 2024 — McMap. All rights reserved.