Request no.3 - not allow shared objects
Original request - May be adopt XMLBeans interface of adding attributes instead of createing them. This way, you will prevent accidental sharing of objects throughout the hierarchy, and even the infinite circular dependencies.
Kohsuke response - Could you elaborate on this?
In JAXB and JAXB 2.0, you use ObjectFactory to create an instance, use setters to set the attributes, and then use setter of the containing class to set this instance:
final HeadType head = objFactory.createHeadType();
final PersonType headPerson = objFactory.createPersonType();
In XMLBeans and C24, there is no need to call the final setter:
final HeadType head = org.addNewHead();
final PersonType headPerson = head.addNewPerson();
This prevents the user from "sharing" the same object in different places in the hierarchy. This also prevents circular dependencies in the hierachy (what does JAXB do in this case?). The same goes for collections, the add() function returns the newly created object which was internally added to the underlying collection.
Both XMLBeans and C24 provide the setter function. The implementation of XMLBeans is unavailable (well, you always can decompile it), but in C24, each object stores a link to its father. I don't know how exactly they handle the circular dependencies, but my proposition is:
For every class attribute (both XML attribute and XML subelement), provide create() and get() functions in the class itself (not in the ObjectFactory, the implementation may go to the ObjectFactory, though).
For collections, provide add() and get() function. The get() function should have the word List in it to indicate that it returns a list.
As the ultimate result is an XML string, there should be little objection of having to create instance for each node in the hierarchy. The main argument - when unmarshalling, it's not JAXB's decision to allocate as few objects as possible for the hierarchy. There should be a separate class instance for each XML tag.