Search code examples
javamoxy

Add attribute on @XmlElementWrapper


I have this code below

@XmlRootElement(name = "FNOL")
@XmlAccessorType(XmlAccessType.FIELD)
public class ConversationXML {

    @XmlElementWrapper(name = "ParticipantList")
    @XmlElement(name = "Participant")
    List<ParticipantsXML> participantList;
    @XmlElement
    KeyActionsXML keyActions;
    @XmlElement
    LossDetailsXML lossDetails;
    @XmlElement
    AdditionalLossDetailsXML addLossDetails;
    @XmlElement
    PolicyDetailsXML policyDetails;

    //getter setter

}

and I want to add an attribute to the ParticipantList element

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<FNOL>
    <ParticipantList>
        <Participant inv="" v="" pid="" id=""/>
    </ParticipantList>
    <keyActions inv="" v="" pid="" id="11"/>
    <lossDetails inv="" v="" pid="" id="11"/>
    <addLossDetails inv="" v="" pid="" id="11"/>
    <policyDetails inv="" v="" pid="" id="11"/>
</FNOL>

like this one but i have no idea how to do it.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<FNOL>
    <ParticipantList inv="" v="" pid="" id="">
        <Participant inv="" v="" pid="" id=""/>
    </ParticipantList>
    <keyActions inv="" v="" pid="" id="11"/>
    <lossDetails inv="" v="" pid="" id="11"/>
    <addLossDetails inv="" v="" pid="" id="11"/>
    <policyDetails inv="" v="" pid="" id="11"/>
</FNOL>

Can someone help me with this one :)


Solution

  • You can't, really.

    The real solution is to create your participantList as a class of itself.

    @XmlRootElement(name = "FNOL")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class ConversationXML {
    
        @XmlElement
        ParticipantList participantList;
        @XmlElement
        KeyActionsXML keyActions;
        @XmlElement
        LossDetailsXML lossDetails;
        @XmlElement
        AdditionalLossDetailsXML addLossDetails;
        @XmlElement
        PolicyDetailsXML policyDetails;
    
        //getter setter
    
    }
    
    public class ParticipantList {
    
        @XmlElement(name = "Participant")
        List<ParticipantsXML> participants;
    
        @XmlAttribute 
        String inv;
    
        @XmlAttribute
        String v;
    
        ...
    }
    

    (nitpick: 'v' is a really poor attribute name; if your xml format is fixed, use a different name for your field in java, then set the name in the annotation)