字段和列表之间没有简单的映射.我建议创建一个单独的模型,将XML有效负载反序列化到它,然后再转换为所需的POJO.下面的示例显示了这个想法:
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText;
import java.io.File;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
public class XmlApp {
public static void main(String[] args) throws Exception {
System.out.println(new File(".").getAbsolutePath());
File xml = new File("./src/main/resources/test.xml");
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.setDefaultUseWrapper(false);
xmlMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
TypeReference> type = new TypeReference>() {
};
List infos = xmlMapper.readValue(xml,type);
List items = infos.stream()
.map(NowPlayingInfo::getPropertiesAsMap)
.map(m -> {
ScheduleItem item = new ScheduleItem();
item.setSong(m.get("cue_title"));
item.setDatetime(Long.parseLong(m.get("cue_time_start")));
return item;
}).collect(Collectors.toList());
items.forEach(System.out::println);
}
}
class ScheduleItem {
private String song;
private String artist;
private String cover;
private Long datetime;
//getters,setters,toString
}
class NowPlayingInfo {
private String mountName;
private long timestamp;
private String type;
@JsonProperty("property")
private List properties;
public Map getPropertiesAsMap() {
Map map = new LinkedHashMap<>();
properties.forEach(p -> map.put(p.getName(),StringUtils.strip(p.getValue())));
return map;
}
//getters,toString
}
class Property {
@JacksonXmlText
private String value;
@JacksonXmlProperty(isAttribute = true)
private String name;
//getters,toString
}
以上用于XML打印的应用程序:
ScheduleItem{song='Marine marchande',artist='null',cover='null',datetime=1559761571830}