4

What are the best ways to minimize the pain of implementing a sax parser to build an object tree? Like, in what method do you create java objects and where do you store them, how do you keep and use a stack for complex hierarchies, how do you handle attributes, and how do you handle nested elements? How do you handle character data?

Basically, where does core functionality fit to make life easiest?

gtrak
  • 5,598
  • 4
  • 32
  • 41

1 Answers1

2

I frequently use Dom4j's SAXReader with an ElementHandler to build mini-DOM chunks of a large XML file.

There are a few things to keep in mind:

  1. Call elementPath.getCurrent() to get the actual element in your onEnd().
  2. Remember to call element.detach() at the end of your onEnd() method.
  3. The path you give to addHandler(String, Handler) is NOT an xpath: it's just a '/'-separated Path stack.
John
  • 15,418
  • 12
  • 44
  • 65
Ed Brannin
  • 7,691
  • 2
  • 28
  • 32
  • Forsooth! The Answer Preview handled my link-reference properly, but the posted answer messed it up! Editing... – Ed Brannin Nov 10 '10 at 20:40