Java/JAXB:使用继承对XML进行解组

Java/JAXB:使用继承对XML进行解组,java,jaxb,hierarchy,unmarshalling,Java,Jaxb,Hierarchy,Unmarshalling,我需要帮助注释我的类,这样我就能够正确地将所有属性/元素映射到正确的类 我有以下xml: <?xml version="1.0" encoding="UTF-8"?> <ResourceList xsi:schemaLocation="http://www.example.com CM.xsd" xmlns="http://www.example.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

我需要帮助注释我的类,这样我就能够正确地将所有属性/元素映射到正确的类

我有以下xml:

    <?xml version="1.0" encoding="UTF-8"?>
<ResourceList xsi:schemaLocation="http://www.example.com CM.xsd" xmlns="http://www.example.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Resource xsi:type="customField" lastModifiedDate="2001-12-17T09:30:47.0Z" dataType="Alphanumeric" required="false" id="customField/1" expiryDate="2001-12-17T09:30:47.0Z" expiryReminder="2" targetClass="copyrightHolder" effectiveDate="2001-12-17T09:30:47.0Z" order="0" maximumLength="0" name="a" minimumLength="0" defaultValue="String"/>
    <Resource xsi:type="contact" phoneMobile="String" lastModifiedDate="2001-12-17T09:30:47.0Z" type="a" id="contact/1" phoneOffice="String" email="x@x" expiryDate="2001-12-17T09:30:47.0Z" expiryReminder="2" phoneWork="String" effectiveDate="2001-12-17T09:30:47.0Z" name="a" notify="true"/>
    <Resource xsi:type="document" lastModifiedDate="2001-12-17T09:30:47.0Z" id="document/1" filePath="/docs/document1"/>
    <Resource xsi:type="copyrightHolder" lastModifiedDate="2001-12-17T09:30:47.0Z" type="a" id="copyrightHolder/1" expiryDate="2001-12-17T09:30:47.0Z" expiryReminder="2" effectiveDate="2001-12-17T09:30:47.0Z" name="a" affiliation="a">
        <ExternalId>String</ExternalId>
        <ContactId>contact/1</ContactId>
        <DocumentId>document/1</DocumentId>
        <CustomFieldValue value="String" customFieldId="customField/1"/>
        <Address>String</Address>
    </Resource>
</ResourceList>

一串
联系人/1
文件/1
一串
我希望在流程的最后有一个列表,其中包含属于自定义字段类、联系人类、版权等的对象

下面是模型的设计方法

@XmlSeeAlso({CopyrightHolder.class})
public class AbstractResource {
    private String name;
    private Date creationDate;
    private Date effectiveDate;
    private Date expiryDate;
    private Long dealExpiryReminder;
    private String id;
    private String lastModifiedDate;

    @XmlAttribute
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlAttribute
    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    @XmlAttribute
    public Date getEffectiveDate() {
        return effectiveDate;
    }

    public void setEffectiveDate(Date effectiveDate) {
        this.effectiveDate = effectiveDate;
    }

    @XmlAttribute
    public Date getExpiryDate() {
        return expiryDate;
    }

    public void setExpiryDate(Date expiryDate) {
        this.expiryDate = expiryDate;
    }

    @XmlAttribute
    public Long getDealExpiryReminder() {
        return dealExpiryReminder;
    }

    public void setDealExpiryReminder(Long dealExpiryReminder) {
        this.dealExpiryReminder = dealExpiryReminder;
    }

    @XmlAttribute
    public String getId() {
        return id;
    }

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

    @XmlAttribute
    public String getLastModifiedDate() {
        return lastModifiedDate;
    }

    public void setLastModifiedDate(String lastModifiedDate) {
        this.lastModifiedDate = lastModifiedDate;
    }

}



public class CoreResource extends AbstractResource{

    private List<String> documentIds;
    private List<String> contactIds;
    private String externalId;
    private List<CustomFieldValue> customFieldValues;

    @XmlElement
    public List<String> getDocumentIds() {
        return documentIds;
    }

    public void setDocumentId(List<String> documentIds) {
        this.documentIds = documentIds;
    }

    @XmlElement
    public List<String> getContactIds() {
        return contactIds;
    }

    public void setContactId(List<String> contactId) {
        this.contactIds = contactId;
    }

    @XmlElement
    public String getExternalId() {
        return externalId;
    }

    public void setExternalId(String externalId) {
        this.externalId = externalId;
    }

    @XmlElement
    public List<CustomFieldValue> getCustomFieldValues() {
        return customFieldValues;
    }

    public void setCustomFieldValue(List<CustomFieldValue> customFieldValues) {
        this.customFieldValues = customFieldValues;
    }

}



@XmlRootElement(name="ResourceList", namespace="http://www.example.com")
public class ResourceList {

    private List<AbstractResource> resourceList;


    public Resource[] getResource() {
        // TODO Auto-generated method stub
        return null;
    }

    @XmlElement(name="Resource", namespace="http://www.example.com")
    public List<AbstractResource> getResourceList() {
        return resourceList;
    }


    public void setResourceList(List<AbstractResource> resourceList) {
        this.resourceList = resourceList;
    }

}


public class CopyrightHolder extends CoreResource {

    private String address;
    private String affiliation;

    @XmlElement
    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @XmlAttribute
    public String getAffiliation() {
        return affiliation;
    }

    public void setAffiliation(String affiliation) {
        this.affiliation = affiliation;
    }

}
@xmlseealway({CopyrightHolder.class})
公共类抽象资源{
私有字符串名称;
私人约会;
私人日期生效日期;
私有日期到期日;
私人长期交易;
私有字符串id;
私有字符串lastModifiedDate;
@XmlAttribute
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
@XmlAttribute
公开日期getCreationDate(){
还肌酐;
}
公共无效设置创建日期(创建日期){
this.creationDate=creationDate;
}
@XmlAttribute
公共日期getEffectiveDate(){
返回生效日期;
}
公共无效设置生效日期(日期生效日期){
this.effectiveDate=effectiveDate;
}
@XmlAttribute
公共日期getExpiryDate(){
返回到期日;
}
公共无效设置到期日(到期日){
this.expiryDate=expiryDate;
}
@XmlAttribute
公共长GetDealExpireyReminder(){
返回到期日提醒;
}
公共无效setDealExpiryReminder(长dealExpiryReminder){
this.dealExpiryReminder=dealExpiryReminder;
}
@XmlAttribute
公共字符串getId(){
返回id;
}
公共无效集合id(字符串id){
this.id=id;
}
@XmlAttribute
公共字符串getLastModifiedDate(){
返回最后修改日期;
}
公共void setLastModifiedDate(字符串lastModifiedDate){
this.lastModifiedDate=lastModifiedDate;
}
}
公共类CoreResource扩展了AbstractResource{
私有列表文档ID;
私人名单联系人ID;
私有字符串外部化;
私有列表值;
@XmlElement
公共列表getDocumentId(){
返回文档ID;
}
public void setDocumentId(列出documentid){
this.documentId=documentId;
}
@XmlElement
公共列表getContactId(){
返回联系人ID;
}
公共无效设置联系人ID(列出联系人ID){
this.contactId=contactId;
}
@XmlElement
公共字符串getExternalId(){
回报外部化;
}
公共void setExternalId(字符串externalId){
this.externalId=externalId;
}
@XmlElement
公共列表getCustomFieldValues(){
返回自定义字段值;
}
public void setCustomFieldValue(列出CustomFieldValue){
this.customFieldValues=customFieldValues;
}
}
@XmlRootElement(name=“ResourceList”,命名空间=”http://www.example.com")
公共类资源列表{
私有列表资源列表;
公共资源[]获取资源(){
//TODO自动生成的方法存根
返回null;
}
@XmlElement(name=“Resource”,命名空间=”http://www.example.com")
公共列表getResourceList(){
返回资源列表;
}
public void setResourceList(列表resourceList){
this.resourceList=resourceList;
}
}
公共类版权所有者扩展CoreResource{
私有字符串地址;
私人关系;
@XmlElement
公共字符串getAddress(){
回信地址;
}
公共无效设置地址(字符串地址){
this.address=地址;
}
@XmlAttribute
公共字符串getAffiliation(){
回归归属;
}
公共关系(字符串关系){
这个。从属关系=从属关系;
}
}
当我摄取xml时,正如预期的那样,我得到了一个抽象资源列表,但是我希望对象是他的xml类型。在这个xml中,AbstractResource的第三个元素是版权所有者,第二个元素是文档,依此类推

是否可以仅使用注释来实现这一点?如果没有,我会怎么做


谢谢

以下内容应该会有所帮助:谢谢Blaise的回复。我查看了一下博客,没有找到unmarshall部分。您能否提供帮助,帮助我如何将xml解组到正确的java类?以下内容应该会有所帮助:感谢Blaise的回复。我查看了一下博客,没有找到unmarshall部分。您能否提供帮助,帮助我如何将xml解组到正确的java类?