Reputation: 2865
Suppose I have the following two classes which I wish to marshall using jaxb
@XmlRootElement(name = "VirtualizationData")
public class VirtualizationData
{
@XmlElement(name = "VirtualOrganization")
public List<VirtualOrganization> Organizations = new ArrayList<VirtualOrganization>();
}
and
public class VirtualOrganization
{
public VirtualOrganization(String p_name, String p_id)
{
m_name = p_name;
m_id = p_id;
}
@XmlAttribute(name = "Name")
private String m_name;
@XmlAttribute(name = "Id")
private String m_id;
}
Here's an example of a marshalled xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<VirtualizationData>
<VirtualOrganization Name="localhost" Id="555"/>
</VirtualizationData>
I have two questions -
Thanks.
Update:
Following lexicore's answer, I wanted to clarify my questions a bit more. What bothers me is the verbosity of these annotations and the fact that I'll have the same strings and annotations in different places. The question was asked in the hope of finding a convention-over-configuration style solution to the problem. Is there a way to create such conventions in jaxb?
Upvotes: 0
Views: 431
Reputation: 43651
Short answer: no, there is no (easy) way to change that without annotations because annotations ARE the (easy) way to change that.
Long answer:
virtualizationData
, VirtualizationData
, virtualization-data
would all make sense. But one method had to be chosen by default. If it were, like you're suggesting, VirtualizationData
, you could always ask - why not virtualizationData
?VirtualOrganization
is the generic type parameter of the list. When compiling schemas you can easily get something like List<Serializable>
, but Serializables
would not really make sense as an element name. So we can conclude that name of the collection item class does not necessarily gives a meaningful name for the attribute. I think you can't define the default collection element name on the VirtualOrganization
(if this is what you're asking).I said above, there is no (easy) way to change that. But it is doable. You can write and configure your own annotation reader which would implement different default naming. However I would definitely not recommend doing that. Just use annotations to customize.
Upvotes: 1