简体   繁体   中英

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.

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{...}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM