2015-07-13 48 views
2

我有以下添加屬性上@XmlElementWrapper

@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

你不能,真的。

真正的解決方案是創建您的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; 

    ... 
} 

(雞蛋裏挑骨頭:「V」是一個非常窮的屬性名稱;如果您的XML格式是固定的,在Java中你的領域使用不​​同的名稱,然後設置在註釋中的名稱)