So I have a tree implemented my custom tree structure moreless like this:
The tree class:
@XmlRootElement
public class Tree {
Set<? extends TreeNode> nodes;
@XmlElement
Set<? extends TreeNode> getNodes() {...}
}
The abstract node:
public abstract class Node {
Set<? extends Node> children;
private String name;
@XmlAttribute
public String getName() {...}
@XmlElement
public abstract Set<? extends Node> getChildren();
}
The group (could contain groups and entities):
@XmlRootElement(name = "group")
public class Group extends Node {
private final Set<Group> groups = new HashSet<Group>();
private final Set<Entity> entities = new HashSet<Entity>();
public Set<Group> getGeoups() { ... }
public Set<Entity> getEntities() { ... }
}
The node:
@XmlRootElement(name = "entity")
public class Entity extends Node {
public Set<DashboardNode> getChildren() {
return Collections.<DashboardNode> emptySet();
}
}
My problem is, that JAXB doesn’t use diffrent names for groups and entities. I would like to get result similar to:
<tree>
<node>
<group>
<group>
<entity/>
</group>
</group>
</node>
<node>
<group>
<entity/>
</group>
<group>
</group>
<entity/>
</node>
</tree>
And I get this instead:
<tree>
<node>
<children>
<children>
<children/>
</children>
</children>
</node>
(...)
</tree>
IF i remove the annotation in my abstract node class on the getChildren() method I would reveive only this:
<tree>
<node>
</node>
<node>
</node>
</tree>
Have a look at the
@XmlElementsannotation, adding a suitable one of these to the children property ofNodeshould achieve what you’re after.