我决定让Jackson XML有机会将XML字符串转换为POJO。
所以我使用了以下很棒的web site来从我的xml字符串生成pojo。
因此,它创建了我需要的类,然后我尝试将xml值读入这些对象。
不幸的是,有些值在对象中设置为null,我无法找出原因:
这是xml字符串的开头:
<?xml version="1.0" encoding="UTF-8"?>
<schedule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" generated_at="2018-02-23T16:55:45+00:00" xmlns="http://schemas.thesite.com/jobsapi/v1/myjob" xsi:schemaLocation="http://schemas.thesite.com/jobsapi/v1/myjob http://schemas.thesite.com/asb/myjob/v1/xml/endpoints/myjob/schedule.xsd">
<job_event id="sr:task:12004724" scheduled="2018-02-23T17:00:00+00:00" start_time_tbd="false" status="not_started">
<task type="group" number="23"/>
我所拥有的课程是:
public class Schedule
{
private String generated_at;
private String xmlns;
private Job_event[] job_events;
public String getGenerated_at ()
{
return generated_at;
}
public void setGenerated_at (String generated_at)
{
this.generated_at = generated_at;
}
public String getXmlns ()
{
return xmlns;
}
public void setXmlns (String xmlns)
{
this.xmlns = xmlns;
}
public Job_event[] getjob_events ()
{
return job_events;
}
public void setjob_events (Job_event[] job_events)
{
this.job_events = job_events;
}
@Override
public String toString()
{
return "Schedule [generated_at = "+generated_at+", xmlns = "+xmlns+", job_events = "+job_events+"]";
}
}
和
public class Job_event {
private String id;
private String scheduled;
private String start_time_tbd;
private String status;
//more fields, getters and setters
}
然后,我用它将字符串转换为pojos:
ObjectMapper xmlMapper = new XmlMapper();
xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Schedule sched = xmlMapper.readValue(xmlString, Schedule.class);
Job_event[] job_events = sched.getJob_events();
我在job_events中的每个事件值中都发现成员值如id和status,其余为null。
为什么? xml字符串没问题。
谢谢!
答案 0 :(得分:0)
您可以通过执行以下操作来解决此问题
@JacksonXmlProperty(isAttribute = true)
注释将Pojo字段映射到XML属性List
代替数组@JacksonXmlElementWrapper(useWrapping = false)
注释表示您的列表List<JobEvent>
表示为重复的XML元素。 localName
,以便与Java命名约定保持兼容之后它看起来像
@JsonIgnoreProperties(ignoreUnknown = true)
public class JobEvent {
@JacksonXmlProperty(isAttribute = true)
private String id;
@JacksonXmlProperty(isAttribute = true)
private String scheduled;
@JacksonXmlProperty(isAttribute = true, localName = "start_time_tbd")
private String startTimeTbd;
@JacksonXmlProperty(isAttribute = true)
private String status;
@Override
public String toString() {
return id + " " + scheduled + " " + " " + startTimeTbd + " " + status;
}
}
Schedule.java
public class Schedule {
@JacksonXmlProperty(isAttribute = true,localName="generated_at")
public String generatedAt;
@JacksonXmlProperty(isAttribute = true)
public String xmlns;
@JacksonXmlElementWrapper(useWrapping = false)
public List<JobEvent> job_event;
@Override
public String toString()
{
return "Schedule [generated_at = "+generatedAt+", xmlns = "+xmlns+", job_events = "+job_event.toString()+"]";
}
}
TESTCALL
@Test
public void readXmlToSelfDefinedPojo2() throws Exception {
ObjectMapper mapper = new XmlMapper();
Schedule pojo = mapper.readValue(
Thread.currentThread().getContextClassLoader().getResourceAsStream("48963966.xml"),
Schedule.class);
System.out.println(pojo + "");
}
对于XML文件48963966.xml
将打印
Schedule [generated_at = 2018-02-23T16:55:45+00:00, xmlns = null, job_events = [sr:task:12004724 2018-02-23T17:00:00+00:00 false not_started, sr:task:12004725 2018-02-25T17:00:00+00:00 false not_started]]