Have you considered using a different type of parser? SAX can be used to remember tree structures but that's really a programmers job to put it in. Its main purpose seems to be better for "blind" read once parsing. You should look into something like the DOM parser. It is more interactive and holds the entire tree structure for you so that you can find any individual subtrees that you need.