无法使用 Jackson 解析 XML,模型永远不会被填充
Cannot parse XML using Jackson, model never gets populated
我正在尝试使用 Jackson 库解析波纹管 xml 但没有成功。我对我的模型进行了一些尝试,但没有发现问题...
XML:
<?xml version="1.0" encoding="UTF-8"?>
<litre version="1.1" creator="Dummy Creator"
xsi:schemaLocation="http://www.dummy.com/xmlschemas/DummyExtension.xsd>
<metadata>
<link href="dummy.com">
<text>Dummy</text>
</link>
<time>2012-10-24T23:22:51.000Z</time>
</metadata>
<krt>
<name>Untitled</name>
<geskrt>
<pkrt data1="123" data2="321">
<ints>100</ints>
<time>2012-10-24T22:21:15.000Z</time>
<extensions>
<axtpx:DummyExtension>
<xtpx:hor>40000</xtpx:hor>
</axtpx:DummyExtension>
</extensions>
</pkrt>
<pkrt data1="543" data2="345">
<ints>100</ints>
<time>2012-10-24T23:29:40.000Z</time>
<extensions>
<axtpx:DummyExtension>
<xtpx:hor>50000</xtpx:hor>
</axtpx:DummyExtension>
</extensions>
</pkrt>
</geskrt>
</krt>
</litre>
POJO:
@JacksonXmlRootElement(localName = "litre")
public @Data class Model {
@JacksonXmlProperty(localName = "pkrt")
@JacksonXmlElementWrapper(useWrapping = false)
private List<Data> entries;
public static @Data class Data {
@JacksonXmlProperty(isAttribute = true, localName = "data1")
private double dataOne;
@JacksonXmlProperty(isAttribute = true, localName = "data2")
private double dataTwo;
@JacksonXmlProperty(localName = "ints")
private double inters;
private String time;
@JacksonXmlProperty(namespace = "xtpx:hor")
private long hours;
}
}
尝试反序列化时:
Model model = mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.readValue(filePath.toFile(), Model.class);
解析始终无一例外地通过,但条目列表始终为空...
感谢任何帮助,thanx!
你肯定必须做这样的事情:
@JacksonXmlRootElement(localName = "litre")
public @Data class Litre {
@JacksonXmlProperty(localName = "krt")
private Krt krt;
public static @Data class Krt {
@JacksonXmlProperty(localName = "geskrt")
private Geskrt geskrt;
}
public static @Data class Geskrt {
@JacksonXmlProperty(localName = "pkrt")
@JacksonXmlElementWrapper(useWrapping = false)
private List<Pkrt> pkrt;
}
public static @Data class Pkrt {
@JacksonXmlProperty(isAttribute = true, localName = "data1")
private double dataOne;
@JacksonXmlProperty(isAttribute = true, localName = "data2")
private double dataTwo;
@JacksonXmlProperty(localName = "ints")
private double inters;
private String time;
@JacksonXmlProperty(namespace = "xtpx:hor")
private long hours;
}
}
我正在尝试使用 Jackson 库解析波纹管 xml 但没有成功。我对我的模型进行了一些尝试,但没有发现问题...
XML:
<?xml version="1.0" encoding="UTF-8"?>
<litre version="1.1" creator="Dummy Creator"
xsi:schemaLocation="http://www.dummy.com/xmlschemas/DummyExtension.xsd>
<metadata>
<link href="dummy.com">
<text>Dummy</text>
</link>
<time>2012-10-24T23:22:51.000Z</time>
</metadata>
<krt>
<name>Untitled</name>
<geskrt>
<pkrt data1="123" data2="321">
<ints>100</ints>
<time>2012-10-24T22:21:15.000Z</time>
<extensions>
<axtpx:DummyExtension>
<xtpx:hor>40000</xtpx:hor>
</axtpx:DummyExtension>
</extensions>
</pkrt>
<pkrt data1="543" data2="345">
<ints>100</ints>
<time>2012-10-24T23:29:40.000Z</time>
<extensions>
<axtpx:DummyExtension>
<xtpx:hor>50000</xtpx:hor>
</axtpx:DummyExtension>
</extensions>
</pkrt>
</geskrt>
</krt>
</litre>
POJO:
@JacksonXmlRootElement(localName = "litre")
public @Data class Model {
@JacksonXmlProperty(localName = "pkrt")
@JacksonXmlElementWrapper(useWrapping = false)
private List<Data> entries;
public static @Data class Data {
@JacksonXmlProperty(isAttribute = true, localName = "data1")
private double dataOne;
@JacksonXmlProperty(isAttribute = true, localName = "data2")
private double dataTwo;
@JacksonXmlProperty(localName = "ints")
private double inters;
private String time;
@JacksonXmlProperty(namespace = "xtpx:hor")
private long hours;
}
}
尝试反序列化时:
Model model = mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.readValue(filePath.toFile(), Model.class);
解析始终无一例外地通过,但条目列表始终为空...
感谢任何帮助,thanx!
你肯定必须做这样的事情:
@JacksonXmlRootElement(localName = "litre")
public @Data class Litre {
@JacksonXmlProperty(localName = "krt")
private Krt krt;
public static @Data class Krt {
@JacksonXmlProperty(localName = "geskrt")
private Geskrt geskrt;
}
public static @Data class Geskrt {
@JacksonXmlProperty(localName = "pkrt")
@JacksonXmlElementWrapper(useWrapping = false)
private List<Pkrt> pkrt;
}
public static @Data class Pkrt {
@JacksonXmlProperty(isAttribute = true, localName = "data1")
private double dataOne;
@JacksonXmlProperty(isAttribute = true, localName = "data2")
private double dataTwo;
@JacksonXmlProperty(localName = "ints")
private double inters;
private String time;
@JacksonXmlProperty(namespace = "xtpx:hor")
private long hours;
}
}