Currently I'am facing problem when trying to parse a xml document to a Java Object. The only necessary information which i want to keep is the iid and time list.
xml test file:
<items>
<item>
<iid>14</iid>
<options>
<option>
<times>
<time>
<timeentry>20200714100100</timeentry>
<timetill>20200714101500</timetill>
<timemaxcount>2</timemaxcount>
</time>
<time>
<timeentry>20200714101600</timeentry>
<timetill>20200714103000</timetill>
<timemaxcount>2</timemaxcount>
</time>
<time>
<timeentry>20200714103100</timeentry>
<timetill>20200714104500</timetill>
<timemaxcount>2</timemaxcount>
</time>
<time>
<timeentry>20200714104600</timeentry>
<timetill>20200714110000</timetill>
<timemaxcount>2</timemaxcount>
</time
</option>
</options>
</item>
</items>
I have created two Java Objects classes which contains the iid and the time list. When parsing the xml file only the field iid gets filled and the list object is null. What do I missing ?
@JsonIgnoreProperties(ignoreUnknown = true)
@XmlRootElement(name = "item")
@JacksonXmlRootElement(localName = "item")
public class SubProduct implements Serializable {
private String iid;
@JacksonXmlElementWrapper(localName = "times")
@JacksonXmlProperty(localName = "time")
private List<TimePeriod> times;
}
@JsonIgnoreProperties(ignoreUnknown = true)
@JacksonXmlRootElement(localName = "time")
public class TimePeriod implements Serializable {
@JsonProperty(value = "timeentry")
String timeEntry;
@JsonProperty(value = "timetill")
String timeTill;
@JsonProperty(value = "timemaxcount")
String timeMaxCount;
}
service layer:
...
NodeList itemList = document.getElementsByTagName("item");
List<SubProduct> subProducts = new ArrayList<>();
for (int i = 0; i < nodes.getLength(); i++) {
SubProduct value = xmlMapper.readValue(nodeToString(nodes.item(i)), SubProduct.class);
subProducts.add(value);
}
return subProducts;
...
public static String nodeToString(Node node) throws Exception{
StringWriter sw = new StringWriter();
Transformer t = TransformerFactory.newInstance().newTransformer();
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.transform(new DOMSource(node), new StreamResult(sw));
return sw.toString();
}
reponse:
{
"iid": "9",
"times": null
},