I'm looking for a DOM-style XML parser that doesn't load the entire document. Rather, it would let you get an element, then iterate over the element's children, presenting each child as a DOM node but only loading one child at a time (to conserve memory when there's a large number of children. Where I can get such a parser?
Created May 4, 2012
Chandra Patni You can use JDOM API (See also JSR0102) to achieve this effect. The following snippet of code shows how JDOM can be used to get the
java.util.List
of child(ren) of a node. In JDOM, a node is said to be org.jdom.Element
.
org.jdom.input.SAXBuilder builder = new org.jdom.input.SAXBuilder(); org.jdom.Element root = builder.build(new java.io.File("file.xml")).getRootElement(); java.util.List children = root.getChildren(); //...An
org.jdom.Element
can be convert to org.w3c.dom.Element
by using org.jdom.output.DOMOutputter
class. Moreover, JDOM lets you change the modify the document without having to worry about underlying parser. See also: Parsing a very large XML file FAQ.