在@XmlElementWrapper上添加属性

时间:2015-07-13 11:40:26

标签: java moxy

我有以下代码

@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

}

我想在ParticipantList元素中添加一个属性

<?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>
像这样,但我不知道该怎么做。

<?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>

有人可以帮助我这个:)

1 个答案:

答案 0 :(得分:2)

你不能,真的。

真正的解决方案是将您的participantList创建为自己的类。

@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'是一个非常差的属性名称;如果您的xml格式是固定的,请在java中为您的字段使用不同的名称,然后在注释中设置名称)