JAXB(Grand)子元素数据未填充

时间:2017-08-31 13:29:32

标签: java jaxb

我无法解密(大)子表的元素。这是传入数据的结构:

<searchResultDocuments>
    <pageNumber>1</pageNumber>
    <pageSize>1</pageSize>
    <results>
        <contentType>text/html</contentType>
        <fileName>theFile.txt</fileName>
        <mainDoc>
            <dates>
                <date>
                    <match>20170822</match>
                    <startOffset>4324</startOffset>
                </date>
                ...
            </dates>
            <entities>
                <entity>
                    <startOffsets>4324 5634</startOffsets>
                    <entityType>featuretype</entityType>
                <entity>
                ...
            </entities>
        </mainDoc>
    </results>
    ...
</searchResultDocuments>

捕获数据的java类如下所示:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="searchResultDocuments")
public class SearchReturnData{
    @XmlElement
    private int pageNumber;
    @XmlElement
    private int pageSize;
    @XmlElement(name="results")
    private List<ResultData> resultData;
//(no setters, getters only in all classes, although I tried it both ways)
}// end of SearchResultData

@XmlAccessorType(XmlAccessType.FIELD)
public class ResultData{
    @XmlElement
    private String contentType;
    @XmlElement
    private String fileName;
    @XmlElement(name="mainDoc")
    private MainDoc mainDoc;
} // end of ResultData

@XmlAccessorType(XmlAccessType.FIELD)
public class MainDoc{
    @XmlElement(name="dates")
    private List<DateData>    dates;
    @XmlElement(name="entities")
    private List<EntityData> entities;
}// end of MainDoc

@XmlType(name="date")
public class DateData{
    @XmlElement(name="match")
    private String match;
    @XmlElement(name="startOffset")
    private String startOffset;
}// end of DateData

@XmlType(name="entity")
public class EntityData{
    @XmlElement(name="startOffsets")
    private String startOffsets;
    @XmlElement(name="entityType")
    private String entityType;
}// end of EntityData

当我将传入数据解组到此结构中时,我获得了DataData和EntityData的正确行数,但没有一个元素具有数据。我已经尝试过没有制定者,但没有任何区别。

1 个答案:

答案 0 :(得分:1)

MainDoc课程中,您需要在列表中使用元素包装器。

@XmlElementWrapper(name="dates")
@XmlElement(name="date")
public List<DateData> dates;

@XmlElementWrapper(name="entities")
@XmlElement(name="entity")
public List<EntityData> entities;

此外,您的XML不会关闭<entity>项,它应该包含<entity></entity>中的一个,而不是<entity>中的两个。< / p>