Reputation: 75
I'm having trouble unmarshaling elements of a (grand)child table. Here's the structure of the incoming data:
<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>
The java classes to capture the data look like this:
@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
When I unmarshal the incoming data into this structure, I get the proper number of rows of DataData and EntityData, but none of the elements have data. I've tried having and not having setters but it makes no difference.
Upvotes: 1
Views: 209
Reputation: 1513
In your MainDoc
class, you need to use an element wrapper on your lists.
@XmlElementWrapper(name="dates")
@XmlElement(name="date")
public List<DateData> dates;
@XmlElementWrapper(name="entities")
@XmlElement(name="entity")
public List<EntityData> entities;
Also, your XML doesn't close the <entity>
item, it should have one of <entity>
and one of </entity>
instead of two of <entity>
.
Upvotes: 1