I'm using javax.xml.parsers.DocumentBuilderFactory to load an XML file. The file contains paired elements like so:


So if I did the following:

Element element = <root node element>
NodeList nodeListA = element.getElementsByTagName("A");
NodeList nodeListB = element.getElementsByTagName("B");

Can I assume the elements are in parallel so I can match them as they were written in the XML file like so:

Also, one last question. As I'm currently using the sax parser on a set of test data, the xml file I will be using with this program is over 70MB. Last time I tried to load the file with this program, Java runs out of memory and throws me an error: java.lang.OutOfMemoryError: Java heap space

Is there a more efficient alternative for parsing XML?

The api doc on getElementsByTagName states that it

Returns a NodeList of all descendant Elements with a given tag name, in document order.

So yes, you should be able to pair them directly.

On the second question, you say the sax parser is dying on an out of memory or the dom parser? I wouldn't think the sax parser would have any trouble with that since it's more like a firehose of data and doesn't need to retain the entire model in memory.

Thanks for reading the API closer than I did.

I guess it would be the DOM parser. The fact that DocumentBuilder.parse() can throw a SAXException is what made me think it was SAX parser.

This article has been dead for over six months. Start a new discussion instead.