Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JAXB - How to get parent element in generated classes

843834Jan 19 2010 — edited Feb 10 2010
I've XML document. From XML DTD I've generated Java classes. For example, XML
<root>
	<parent name="parentName">
		<child />
	</parent>
</root>
gives me classes Root.java, Parent.java (witd List<Child> getChild() method) and Child.java. So now I can call unmarshall:
JAXBContext jc = JAXBContext.newInstance("mygeneratedpackage");
Unmarshaller unmarshaller = jc.createUnmarshaller();
Root root = (Root) unmarshaller.unmarshal(new File("my.xml"));

List parentList = root.getParent();
Iterator iterator = parentList.iterator();
while ( iterator.hasNext() ){
	...
}
I can iterate to Child. But what if I want to go back from Child to Parent? Is there any way how to generate pointer from child to parent? I've found afterUnmarshal callback, so I can add
@XmlTransient
private Parent parent;

...

public void afterUnmarshal(Unmarshaller u, Object parent) {
    this.parent = (Parent) parent;
} 
to Child.java class, but I have to add this manually after each generatig Java classes from XML DTD file. Is there any way how to do it automatically?

Thanks for any help.


Sorry for my poor English.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 10 2010
Added on Jan 19 2010
3 comments
2,200 views