Java xml解组列表嵌套对象问题
我有一个包含以下内容的xml:Java xml解组列表嵌套对象问题,java,xml,Java,Xml,我有一个包含以下内容的xml: <ParentClass> <StringA>A</StringA> <StringB>B</StringB> <Items> <Item ts="2016-03-25T20:00:00+02:00">1.17</Item> <Item ts="2016-03-25T21:00:00+02:00">1.
<ParentClass>
<StringA>A</StringA>
<StringB>B</StringB>
<Items>
<Item ts="2016-03-25T20:00:00+02:00">1.17</Item>
<Item ts="2016-03-25T21:00:00+02:00">1.15</Item>
</Items>
</ParentClass>
A.
B
1.17
1.15
我想读它,但是我被正确的映射卡住了。课程如下:
@XmlAccessorType(XmlAccessType.FIELD)
@NoArgsConstructor
@AllArgsConstructor
@Data
public class ParentClass {
@XmlElement(name = "StringA", required = true)
private String a;
@XmlElement(name = "StringB", required = true)
private String b;
@XmlElement(name = "Items", required = true)
private List<Item> consumptionList;
}
@NoArgsConstructor
@AllArgsConstructor
@Data
@XmlAccessorType(XmlAccessType.FIELD)
public class Item {
@XmlAttribute(name = "ts", required = true)
@XmlJavaTypeAdapter(value = LocalDateTimeAdapter.class)
private LocalDateTime timestamp;
private double value; //this corrensponds to 1.17 and 1.15 in xml
}
@xmlacessortype(xmlacesstype.FIELD)
@诺尔格构装师
@AllArgsConstructor
@资料
公共类父类{
@xmlement(name=“StringA”,required=true)
私人字符串a;
@xmlement(name=“StringB”,required=true)
私有字符串b;
@xmlement(name=“Items”,required=true)
私人消费清单;
}
@诺尔格构装师
@AllArgsConstructor
@资料
@XmlAccessorType(XmlAccessType.FIELD)
公共类项目{
@xmltattribute(name=“ts”,required=true)
@XmlJavaTypeAdapter(值=LocalDateTimeAdapter.class)
私有LocalDateTime时间戳;
private double value;//该值对应于xml中的1.17和1.15
}
在实际的文件中有100多个条目,但当我阅读它时,列表中只填充了一个条目类实例,并且两个字段都为null
我猜Item类中的映射都是错误的,但是尝试了all,似乎什么都不起作用
我应该如何正确地映射它以实现目标?您需要将
@XmlValue
添加到字段value
,否则它默认为@XmlElement
此外,您需要将consumptionList
上的注释更改为
@XmlElementWrapper(name = "Items")
@XmlElement(name = "Item", required = true)
还要注意,ts
值是OffsetDateTime
(或ZonedDateTime
)值,而不是LocalDateTime
,除非LocalDateTimeAdapter
应用时区,例如JVM默认时区
我发现,帮助正确应用@Xml…
注释的最佳方法是创建对象并将其封送到Xml以查看您得到了什么。您当前的代码将创建以下XML:
A.
B
1.17
1.15
如果应用上述更改,您将获得:
A.
B
1.17
1.15
通过添加以下代码创建了上述输出:
class LocalDateTimeAdapter扩展了XmlAdapter{
@凌驾
公共字符串封送处理(LocalDateTime)引发异常{
返回时间。在区域(区域偏移小时数(2))
.format(模式的DateTimeFormatter.of(“uuu-MM-dd'HH:MM:ssxxx”);
}
@凌驾
公共LocalDateTime解组器(字符串文本)引发异常{
返回ZonedDateTime.parse(text.toLocalDateTime();
}
}
publicstaticvoidmain(String…args)引发异常{
ParentClass p=新的父类(“A”,“B”,Arrays.asList(
新项目(LocalDateTime.parse(“2016-03-25T20:00:00”),1.17,
新项目(LocalDateTime.parse(“2016-03-25T21:00:00”),1.15);
JAXBContext JAXBContext=JAXBContext.newInstance(ParentClass.class);
Marshaller=jaxbContext.createMarshaller();
setProperty(marshaller.JAXB_格式的_输出,Boolean.TRUE);
马歇尔(p,系统输出);
}
您需要将@XmlValue
添加到字段value
,否则默认为@XmlElement
此外,您需要将consumptionList
上的注释更改为
@XmlElementWrapper(name = "Items")
@XmlElement(name = "Item", required = true)
还要注意,ts
值是OffsetDateTime
(或ZonedDateTime
)值,而不是LocalDateTime
,除非LocalDateTimeAdapter
应用时区,例如JVM默认时区
我发现,帮助正确应用@Xml…
注释的最佳方法是创建对象并将其封送到Xml以查看您得到了什么。您当前的代码将创建以下XML:
A.
B
1.17
1.15
如果应用上述更改,您将获得:
A.
B
1.17
1.15
通过添加以下代码创建了上述输出:
class LocalDateTimeAdapter扩展了XmlAdapter{
@凌驾
公共字符串封送处理(LocalDateTime)引发异常{
返回时间。在区域(区域偏移小时数(2))
.format(模式的DateTimeFormatter.of(“uuu-MM-dd'HH:MM:ssxxx”);
}
@凌驾
公共LocalDateTime解组器(字符串文本)引发异常{
返回ZonedDateTime.parse(text.toLocalDateTime();
}
}
publicstaticvoidmain(String…args)引发异常{
ParentClass p=新的父类(“A”,“B”,Arrays.asList(
新项目(LocalDateTime.parse(“2016-03-25T20:00:00”),1.17,
新项目(LocalDateTime.parse(“2016-03-25T21:00:00”),1.15);
JAXBContext JAXBContext=JAXBContext.newInstance(ParentClass.class);
Marshaller=jaxbContext.createMarshaller();
setProperty(marshaller.JAXB_格式的_输出,Boolean.TRUE);
马歇尔(p,系统输出);
}