如何使用JaxB在@XmlRootElement中添加新元素

时间:2014-06-11 12:58:00

标签: jaxb

我是jaxb的新手。我有一个场景我需要使用JAXB生成以下XML

<WCHECK>
     <ALIASES>
        <ENTRY>
            <ENABLED>No</ENABLED>
            <TYPE>AKA</TYPE>
            <TEXT>AL1 JAMMALI IMAD BEN BECHIR BEN HAMDA</TEXT>
            <NATIVE>Yes</NATIVE>
        </ENTRY> 
        <ENTRY>
            <ENABLED>Yes</ENABLED>
            <TYPE>AKA</TYPE>
            <TEXT>AL2 JAMMALI IMAD BEN BECHIR BEN HAMDA</TEXT>
            <NATIVE>Yes</NATIVE>
        </ENTRY> 
     </ALIASES>
     <PERMUTATIONS>
        <ENTRY>
            <ENABLED>No</ENABLED>       
            <TEXT>Jammali Hamda</TEXT>
        </ENTRY> 
     </PERMUTATIONS>
</WCHECKS>

我为同一个

创建了Allias.java和Permussions.java
@Entity
@Table(name="ING_ALIAS")
public class Aliases {

    private String id;
    private String type;
    private String text;
    private boolean enabled;
    private boolean isNativeAllias;
    private VwatchDetails  vwatchDetails;

    @Id
    @Column(name="ID")
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Column(name="KA_TYPE")
    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Column(name="TEXT")
    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ENTRY_ID", nullable = false)
    public VwatchDetails getVwatchDetails() {
        return vwatchDetails;
    }

    public void setVwatchDetails(VwatchDetails vwatchDetails) {
        this.vwatchDetails = vwatchDetails;
    }

    @Column(name="IS_ENABLED")
    public boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    @Column(name="IS_NATIVE_ALIAS")
    public boolean getIsNativeAllias() {
        return isNativeAllias;
    }

    public void setIsNativeAllias(boolean isNativeAllias) {
        this.isNativeAllias = isNativeAllias;
    }

此类生成xml文件,但缺少ENTRY&gt;标签。我想知道如何为Allias和Permutations的每条记录添加ENTRY标签。请帮助我。在此先感谢

1 个答案:

答案 0 :(得分:0)

您可以使用@XmlElementWrapper注释添加分组元素。然后使用@XmlElement注释来控制与集合中每个项目对应的元素名称。

@XmlRootElement(name="WCHECK")
@XmlAccessorType(XmlAccessType.FIELD)
public class WCheck

    @XmlElementWrapper(name="ALIASES")
    @XmlElement(name="ENTRY")
    private List<Alias> aliases;

    @XmlElementWrapper(name="PERMUTATIONS")
    @XmlElement(name="ENTRY")
    private List<Permutation> permutations;

}