I need to process a bunch of very large XML files and read each element depth-first. Due to size, any \[code\]DOM\[/code\] solution is out of question and things are further complicated by the fact that the actual element needed is not the "leaf" but its parent.More specifically, the files have a structure like\[code\] <Level 1> ... <Level 2> ... <Level N-1> <value>...</value> <value>...</value> ... <value>...</value> </Level N-1> <Level N-1> <value>...</value> <value>...</value> ... <value>...</value> </Level N-1> ... <Level N-1> <value>...</value> <value>...</value> ... <value>...</value> </Level N-1> ... </Level 2> </Level 1>\[/code\]Out of each file like the above, the \[code\]<Level N-1>\[/code\] elements need to be read individually (each including all the corresponding \[code\]<value>\[/code\] elements). The depth, \[code\]N\[/code\], varies within each file and across files, so it is essentially unknown, as are \[code\]XML\[/code\] tag names.A quick solution for reading an entire XML element at a specific depth as a string is something like\[code\]int level = 0; // The base level of the element, could be at any depthReader in = ... // The reader to the inputByteArrayOutputStream outStream = new ByteArrayOutputStream();PrintStream out = new PrintStream(outStream);XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(in);XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(out);XMLEvent event;while ((level > 0) && reader.hasNext());{ event = reader.nextEvent(); if (event.isStartElement()) { level++; } else if (event.isEndElement()) { level--; } writer.add(event);}writer.flush();String element = new String(outStream.toByteArray());\[/code\]The above, however, is not helpful if the calling code does not know that a \[code\]Level N-1\[/code\] element has been reached and it advances to \[code\]Level N\[/code\] (i.e., to \[code\]<value>\[/code\] elements).A \[code\]SAX\[/code\] solution would be ideal, but even preprocessing the file via an \[code\]XSLT\[/code\] template is acceptable.Any ideas?