JAXB Unmarshaller没有正确处理XML中的列表

时间:2018-03-15 14:53:54

标签: java jaxb unmarshalling

我正在使用JAXB Unmarshaller将XML字符串转换为Java对象结构(使用Java 7; Java 8目前不是一个选项)。我遇到了对象列表的问题。 (我提前为所有包含的代码道歉。)这是输入XML字符串:

<claimImportCompositeDTO>
    <claimId>900</claimId>
    <claimNumber>AT0000000000018</claimNumber>
    <claimDTO>
        <recordId>900</recordId>
        <version>1</version>
        <clmAddress>
            <recordId>900</recordId>
            <version>0</version>
        </clmAddress>
        <claimStatus>
            <expired class="list">
                <claimStatusDTO>
                    <recordId>900</recordId>
                    <version>1</version>
                </claimStatusDTO>
                <claimStatusDTO>
                    <recordId>910</recordId>
                    <version>2</version>
                </claimStatusDTO>
            </expired>
            <statusCompleteWorkItemFlag>false</statusCompleteWorkItemFlag>
        </claimStatus>
    </claimDTO>
    <notes class="list">
        <notepadDTO>
            <recordId>3503</recordId>
            <version>0</version>
            <notepadText class="list">
                <notepadTextDTO>
                    <recordId>3503</recordId>
                    <version>0</version>
                </notepadTextDTO>
                <notepadTextDTO>
                    <recordId>3504</recordId>
                    <version>0</version>
                </notepadTextDTO>
            </notepadText>
        </notepadDTO>
    </notes>
</claimImportCompositeDTO>

这是各种对象的定义:

import java.util.List;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class ClaimImportCompositeDTO {
    private String _claimId;
    private String _claimNumber;
    private ClaimDTO _claimDTO;
    private List<NotepadDTO> _notes;

    public String getClaimId() {
        return _claimId;
    }

    public String getClaimNumber() {
        return _claimNumber;
    }

    public ClaimDTO getClaimDTO() {
        return _claimDTO;
    }

    public List<NotepadDTO> getNotes() {
        return _notes;
    }

    public void setClaimId(String claimId) {
        this._claimId = claimId;
    }

    public void setClaimNumber(String claimNumber) {
        this._claimNumber = claimNumber;
    }

    public void setClaimDTO(ClaimDTO claimDTO) {
        this._claimDTO = claimDTO;
    }

    public void setNotes(List<NotepadDTO> notes) {
        this._notes = notes;
    }
}


public class ClaimDTO {
    private String _recordId;
    private String _version;
    private ClmAddress _clmAddress;
    private ClaimStatus _claimStatus;

    public String getRecordId() {
        return _recordId;
    }

    public String getVersion() {
        return _version;
    }

    public ClmAddress getClmAddress() {
        return _clmAddress;
    }

    public ClaimStatus getClaimStatus() {
        return _claimStatus;
    }

    public void setRecordId(String recordId) {
        this._recordId = recordId;
    }

    public void setVersion(String version) {
        this._version = version;
    }

    public void setClmAddress(ClmAddress clmAddress) {
        this._clmAddress = clmAddress;
    }

    public void setClaimStatus(ClaimStatus claimStatus) {
        this._claimStatus = claimStatus;
    }
}


public class ClmAddress {
    private String _recordId;
    private String _version;

    public String getRecordId() {
        return _recordId;
    }

    public String getVersion() {
        return _version;
    }

    public void setRecordId(String recordId) {
        this._recordId = recordId;
    }

    public void setVersion(String version) {
        this._version = version;
    }
}


import java.util.List;

public class ClaimStatus {
    private List<ClaimStatusDTO> _expired;
    private boolean _statusCompleteWorkItemFlag;

    public List<ClaimStatusDTO> getExpired() {
        return _expired;
    }

    public boolean isStatusCompleteWorkItemFlag() {
        return _statusCompleteWorkItemFlag;
    }

    public void setExpired(List<ClaimStatusDTO> expired) {
        this._expired = expired;
    }

    public void setStatusCompleteWorkItemFlag(boolean statusCompleteWorkItemFlag) {
        this._statusCompleteWorkItemFlag = statusCompleteWorkItemFlag;
    }
}


public class ClaimStatusDTO {
    private String _recordId;
    private String _version;

    public String getRecordId() {
        return _recordId;
    }

    public String getVersion() {
        return _version;
    }

    public void setRecordId(String recordId) {
        this._recordId = recordId;
    }

    public void setVersion(String version) {
        this._version = version;
    }
}


import java.util.List;

public class NotepadDTO {
    private String _recordId;
    private String _version;
    private List<NotepadTextDTO> _notepadText;

    public List<NotepadTextDTO> getNotepadText() {
        return _notepadText;
    }

    public String getRecordId() {
        return _recordId;
    }

    public String getVersion() {
        return _version;
    }

    public void setNotepadText(List<NotepadTextDTO> notepadText) {
        this._notepadText = notepadText;
    }

    public void setRecordId(String recordId) {
        this._recordId = recordId;
    }

    public void setVersion(String version) {
        this._version = version;
    }
}


public class NotepadTextDTO {
    private String _recordId;
    private String _version;

    public String getRecordId() {
        return _recordId;
    }

    public String getVersion() {
        return _version;
    }

    public void setRecordId(String recordId) {
        this._recordId = recordId;
    }

    public void setVersion(String version) {
        this._version = version;
    }
}

这是输出:

<claimImportCompositeDTO>
    <claimId>900</claimId>
    <claimNumber>AT0000000000018</claimNumber>
    <claimDTO>
        <recordId>900</recordId>
        <version>1</version>
        <clmAddress>
            <recordId>900</recordId>
            <version>0</version>
        </clmAddress>
        <claimStatus>
            <expired class="list">
                <claimStatusDTO>
                    <recordId>null</recordId>
                    <version>null</version>
                </claimStatusDTO>
            </expired>
            <statusCompleteWorkItemFlag>false</statusCompleteWorkItemFlag>
        </claimStatus>
    </claimDTO>
    <notes class="list">
        <notepadDTO>
            <recordId>null</recordId>
            <version>null</version>
            <notepadText class="list"/>
        </notepadDTO>
    </notes>
</claimImportCompositeDTO>

当我运行unmarshaller时,会正确读取非列表数据,但会跳过列表中的任何内容。好吧,创建了一个对象(无论列表中有多少个),并且所有字段都为空(即,claimStatus - &gt;过期列表或注释列表)。

我希望我遗漏一些相当基本的东西,但我一直无法找到可能的东西。有什么想法吗?

2 个答案:

答案 0 :(得分:2)

问题是提供的xml与POJO的结构不对应。根据您现有的设置和编码样式,一种简单的方法是在项目列表的字段上添加@XmlElementWrapper并更改其名称。您需要将以下3个类更改为它们在下面的显示方式,并且它将起作用:

@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
public class ClaimImportCompositeDTO {
    private String _claimId;
    private String _claimNumber;
    private ClaimDTO _claimDTO;
    private List<NotepadDTO> _notepadDTO;

    public String getClaimId() {
        return _claimId;
    }

    public String getClaimNumber() {
        return _claimNumber;
    }

    public ClaimDTO getClaimDTO() {
        return _claimDTO;
    }

    @XmlElementWrapper(name = "notes")
    public List<NotepadDTO> getNotepadDTO() {
        return _notepadDTO;
    }

    public void setClaimId(String claimId) {
        this._claimId = claimId;
    }

    public void setClaimNumber(String claimNumber) {
        this._claimNumber = claimNumber;
    }

    public void setClaimDTO(ClaimDTO claimDTO) {
        this._claimDTO = claimDTO;
    }

    public void setNotepadDTO(List<NotepadDTO> notes) {
        this._notepadDTO = notes;
    }
}

ClaimStatus类将是:

@XmlAccessorType(XmlAccessType.PROPERTY)
public class ClaimStatus {


    private List<ClaimStatusDTO> _claimStatusDTO;
    private boolean _statusCompleteWorkItemFlag;

    @XmlElementWrapper(name = "expired")
    public List<ClaimStatusDTO> getClaimStatusDTO() {
        return _claimStatusDTO;
    }

    public void setClaimStatusDTO(List<ClaimStatusDTO> claimStatusDto) {
        this._claimStatusDTO = claimStatusDto;
    }

    public boolean isStatusCompleteWorkItemFlag() {
        return _statusCompleteWorkItemFlag;
    }


    public void setStatusCompleteWorkItemFlag(boolean statusCompleteWorkItemFlag) {
        this._statusCompleteWorkItemFlag = statusCompleteWorkItemFlag;
    }
}

和NotepadDTO:

@XmlAccessorType(XmlAccessType.PROPERTY)
public class NotepadDTO {
    private String _recordId;
    private String _version;
    private List<NotepadTextDTO> _notepadTextDTO;

    @XmlElementWrapper(name = "notepadText")
    public List<NotepadTextDTO> getNotepadTextDTO() {
        return _notepadTextDTO;
    }

    public String getRecordId() {
        return _recordId;
    }

    public String getVersion() {
        return _version;
    }

    public void setNotepadTextDTO(List<NotepadTextDTO> notepadText) {
        this._notepadTextDTO = notepadText;
    }

    public void setRecordId(String recordId) {
        this._recordId = recordId;
    }

    public void setVersion(String version) {
        this._version = version;
    }
}

答案 1 :(得分:0)

您需要列表中的@XmlElement注释。这样的事情。

@XmlElement(name = "notepadDTO")
private List<NotepadDTO> _notes;

这是我用过的教程: https://howtodoinjava.com/jaxb/jaxb-exmaple-marshalling-and-unmarshalling-list-or-set-of-objects/