How to generate two different structure xml from one class using JAXB
I have two class, Node and Group, the Group is Node's sub-class.
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Node", propOrder = {
"id",
"children"
})
public class Node {
protected int id;
protected List<Node> children;
}
then Group class
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Group", propOrder = {
"name"
})
public class Group extends Node{
protected String name;
}
I had define a class Tree to contain the Node class so after the marshal, xml will be
<tree>
<children>
<id>1</id>
<children>
<id>2</id>
</children>
<children>
<id>3</id>
</children>
</children>
</tree>
There is another class Groups to contain all group in array, so I want the xml should be
<groups>
<groups>
<name>group-1</name>
</groups>
<groups>
<name>group-2</name>
</groups>
<groups>
<name>group-3</name>
</groups>
</groups>
But because the Group inherited Node, so the second开发者_JAVA技巧 one always generate xml like this
<groups>
<groups>
<id>group-1</id>
<name>group-1</name>
<children>
<id>2</id>
<name>group-2</name>
</children>
<children>
<id>3</id>
<name>group-3</name>
</children>
<groups>
<groups>
How should I treat the two different class to different xml file even if they are inherited related. I need the object to handle things with current inherited design. So the inherited can't be removed.
If I add a @XmlTransient to Node's children property, the second xml will be the one I want, but the first xml can't generate as the one I want.
I'm confusing now and don't know how to do it.
Any suggestion is welcome.
Thanks in advance.
You can have multiple XML representations for an object model by leveraging an extension in EclipseLink JAXB (MOXy). This extension allows you to provide additional mappings via an XML representation. For more information see:
- http://wiki.eclipse.org/EclipseLink/Examples/MOXy/EclipseLink-OXM.XML
精彩评论