I have the following xml fragment (which is outside of my control):
<item>
<name>Name</name>
<category>1</category>
<subcategory>2</subcategory>
</item>
I'd like <subcategory>
to be unmarshalled to a class that looks like this:
public class SubCategory {
private Category parent;
private int code;
private String name;
public SubCategory(int code, Category parent){
this.code = code;
this.parent = parent;
this.name = lookupName(code);
}
// Getters, setters and lookupName(int) here
}
In other words, the object resulting from the <category>
tag should be passed to the object resulting from the <subcategory>
element.
How can I do this? Is this even possible? I know about XmlAdapter
, but I see no way to get hold of the parent.
I'd prefer it if it can be done in the constructor, so an instance can never be in an invalid state, but I'll settle for a different solution, as long as I don't have to manually set the parent for each SubCategory
.