paril
paril

Reputation: 1970

JAXB marshalling @XmlElementRef name property not coming as Name

I am not getting name as tag name for @XmlElementRef.

Element declaration in parent elemnet:

@XmlElementRef(name = "Agents",  type = JAXBElement.class, required = false)
protected JAXBElement<ArrayOfGeneralAgent> agents;

Declaration of class ArrayOfGeneralAgent:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Agents", propOrder = {
    "generalAgent"
})
public class ArrayOfGeneralAgent {

@XmlElement(name = "GeneralAgent", nillable = true)
        protected List<GeneralAgent> generalAgent;

...
}

From Above code expecting below xml:

<Agents>
  <GeneralAgent>
    <ComPerc>1.5</ComPerc>
    <CustID>abc</CustID>
    <SharePerc>123</SharePerc>
  </GeneralAgent>
</Agents>

But getting XML like this:

<ArrayOfGeneralAgent>
  <GeneralAgent>
    <ComPerc>1.5</ComPerc>
    <CustID>abc</CustID>
    <SharePerc>123</SharePerc>
  </GeneralAgent>
</ArrayOfGeneralAgent>

Instead of "Agents" getting "ArrayOfGeneralAgent".

I tried to many things but nothing found.

Upvotes: 0

Views: 1412

Answers (1)

dirbacke
dirbacke

Reputation: 3019

Your problem is that @XmlElementRef.name does not work for @XmlRootElement, read about this here.

What you have to do is to remove the attribute name from XmlElementRef on your xml element.

@XmlElementRef(type = JAXBElement.class, required = false)
protected JAXBElement<ArrayOfGeneralAgent> agents;

and add @XmlRootElement to your class.

@XmlRootElement(name="Agents")
public class ArrayOfGeneralAgent{...}

Upvotes: 1

Related Questions