Vadim
Vadim

Reputation: 2865

jaxb default element naming in lists

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 -

  1. If I remove the name part of the @XmlRootElement attribute - VirtualizationData becomes virtualizationData in the XML. Is there a way to change that? After all, why not use the class name as the default?
  2. If I remove the name part of the @XmlElement attribute - VirtualOrganization becomes Organizations. Again, why not use the class name as the element name? What if I'll need to have many other classes that will have lists of VirtualOrganization, should I specify the same name in all of them or is there a way to mark VirtualOrganization?

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

Answers (1)

lexicore
lexicore

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:

  1. There definitely are may ways to create element names from Java names (for instance, from the class name here). 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?
  2. The same thing here, but with an additional complexity that 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

Related Questions