>> I am not seeing an easy way to detect where I am in a nested
>> structure (e.g. a house has a room that has a cabinet that has a
>> name v. a person has a name).
>
> Push the elements onto a stack as you go. Peek at the stack to see the
> context you're in. Pop when you reach the closing tag.
You might also explore the jaxb parser library.
Often it is enough just to refer the enclosing element (or its name).
class ElementRepresentation
{
ElementRepresentation encloser;
String tag;
...
}
Then if you want to distinguish, say, person.name from organization.name you
can put code in the "name" handler like:
ElementRepresentation name;
...
if ( name.getEncloser().getTag().equals( "person" ) )
{ ... }
else if ( name.getEncloser().getTag().equals( "organization" ) )
{ ... }
...
Crude, but effective. If necessary you can follow the encloser chain through
more than one layer -
if ( name.getEncloser().getEncloser().getTag().equals( "supervisor" )) ...
I've implemented a few projects using SAX parsing and this type of strategy,
where DOM parsing or XSL seemed like overkill. SAX parsing can process an XML
document in a single pass, and can be amazingly fast. The downside is that it
hard-codes schema structure into the logic, not always a Bad Thing but a
limitation nonetheless.
- Lew