Skip to main content

Tree navigation

5 replies [Last post]
sdp198
Offline
Joined: 2005-04-06
Points: 0

I was wondering if there was a way for class generated by JAXB2 to find its parent in the data tree?

I'm trying to write functions to find the target of an XPath reference. To resolve a relative path I need to be able to move both up and down the data tree created by the unmarshalling.

At the moment I'm planning to add a parent attribute to the superclass of the generated classes, and then walk the tree immediately after unmarshalling setting this attribute. However if there's an easier way to do this I'd be grateful for any hints.

Thanks,

Steve

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
sdp198
Offline
Joined: 2005-04-06
Points: 0

Thank you - that's very useful. I shouldn't have any problems with the pointers not changing as I'm not modifying the data tree.

I have another question if you don't mind though, if I have a JAXBElement of some description is there a way to determine its parent in the tree? I'm asking because I'd like to be able to go up even further in the tree if possible.

Thanks,

Steve

kohsuke
Offline
Joined: 2003-06-09
Points: 0

Aaa, good point.

I can't think of any easy way.

You can define a class that extends JAXBElement, and you can have a parent pointer there (and implement ObjectLifeCycle), then you can override all the createXXX methods on ObjectFactory to return your own JAXBElementEx class (yuck!).
Finally you can then set that ObjectFactory instance to the unmarshaller, so that it will use it for creating objects.

Or maybe I should pass in the stack of parent objects (not just the immediate parent), so that you can build parent pointers by jumping JAXBElement?

Or maybe I should define a listener on the unmarshaller so that you can capture the object hierachy outside the beans?

sdp198
Offline
Joined: 2005-04-06
Points: 0

I'd quite like the stack of pointers being passed in :-)

Tomorrow I'll try extending JAXBElement and see how that goes. If it looks too messy I'll walk down the tree setting parents - speed doesn't matter at the moment so spending time doing that isn't a problem.

Thanks for your replies,

Steve

sdp198
Offline
Joined: 2005-04-06
Points: 0

I tried extending JAXBElement, but despite changing the ObjectFactories I got class cast exceptions from java when I tried to cast the output from unmarshall() into the extended class. I'm using the tree walking option now - it also allows me to print out some useful information on the structure of the tree at the same time.

kohsuke
Offline
Joined: 2003-06-09
Points: 0

Consider using the ObjectLifeCycle interface. It allows each object to receive a parent pointer immediately after that object is unmarshalled.

It's the excellent way to get the parent pointer set up (although the generated code won't help you maintain them as you modify the content tree.)

Search the forum or the JAXB users maling list and I believe you'll find more discussions about ObjectLifeCycle.

I think generating the code that maintains the parent pointer would make a nice plug-in...