将 XML 字符串解析为 class 对象
Parse XML string to class object
我有一个像这样的 XML 字符串:
<?xml version="1.0" encoding="UTF-8" standalone="yes">
<Result xmlns="http://example.com/entities">
<published>2022-02-17T14:26:54.000+01:00</published>
<Id>10</Id>
<QueryResults>
<Id>11</Id>
<created>2022-02-17T12:15:54.000+01:00</created>
</QueryResults>
<QueryResults>
<Id>12</Id>
<created>2022-02-17T12:16:54.000+01:00</created>
</QueryResults>
</Result>
public class ResultModel {
@JsonProperty("Id")
private int id;
@JsonProperty("published")
private Date published;
@JsonProperty("QueryResults")
private List<QueryResult>
// GETTERS and SETTERS
}
public class QueryResult {
@JsonProperty("Id")
private int id;
@JsonProperty("created")
private Date created;
}
当我尝试将字符串读入这样的对象时
try {
com.fasterxml.jackson.dataformat.xml.XmlMapper mapper = new XmlMapper();
ResultModel obj = mapper.readValue(xmlString, ResultModel.class)
} catch (JsonProcessingException e) {
e.printStackTrace();
}
我收到以下错误:
com.fasterxml.jackson.databind.exc.MisMatchedInputException: Cannot construct instance of `a.b.c.d.ResultModel` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ("11")
at [Source: (StringReader); line 1, column 15] (through reference chain: a.b.c.d.ResultModel["QueryResults"]->java.util.ArrayList[0]
我在 class 中使用 @JsonProperty,因为使用 @XmlElement 我还有其他问题。如果我在我的 ResultModel class 中排除字段 QueryResults,那么字符串的解析工作并且看起来正确。我在这里做错了什么?我对解析 Java 中的 XML 字符串非常陌生,所以我希望有一些简单的方法可以解决这个问题...
您需要使用以下 2 个注释:
@JacksonXmlElementWrapper(useWrapping=false)
@JacksonXmlProperty(localName = "QueryResults")
@Data
static class ResultModel {
@JsonProperty("Id")
private int id;
@JsonProperty("published")
private Date published;
@JacksonXmlElementWrapper(useWrapping=false)
@JacksonXmlProperty(localName = "QueryResults")
private List<QueryResult> queryResult;
}
@Data
static class QueryResult {
@JsonProperty("Id")
private int id;
@JsonProperty("created")
private Date created;
}
public static void main(String[] argz) throws JsonProcessingException {
String xml = "<Result>\n"
+ " <published>2022-02-17T14:26:54.000+01:00</published>\n"
+ " <Id>10</Id>\n"
+ " <QueryResults>\n"
+ " <Id>11</Id>\n"
+ " <created>2022-02-17T12:15:54.000+01:00</created>\n"
+ " </QueryResults>\n"
+ " <QueryResults>\n"
+ " <Id>12</Id>\n"
+ " <created>2022-02-17T12:16:54.000+01:00</created>\n"
+ " </QueryResults>\n"
+ "</Result>";
XmlMapper mapper = new XmlMapper();
ResultModel obj = mapper.readValue(xml, ResultModel.class);
System.out.println(obj);
}
控制台:
ResultModel(id=10, published=Thu Feb 17 14:26:54 CET 2022, queryResult=[QueryResult(id=11, created=Thu Feb 17 12:15:54 CET 2022), QueryResult(id=12, created=Thu Feb 17 12:16:54 CET 2022)])
已更新:
Lists and arrays are "wrapped" by default, when using Jackson
annotations, but unwrapped when using JAXB annotations (if supported,
see below) @JacksonXmlElementWrapper.useWrapping can be set to 'false'
to disable wrapping JacksonXmlModule.setDefaultUseWrapper() can be
used to specify whether "wrapped" or "unwrapped" setting is the
default
https://github.com/FasterXML/jackson-dataformat-xml#known-limitations
我有一个像这样的 XML 字符串:
<?xml version="1.0" encoding="UTF-8" standalone="yes">
<Result xmlns="http://example.com/entities">
<published>2022-02-17T14:26:54.000+01:00</published>
<Id>10</Id>
<QueryResults>
<Id>11</Id>
<created>2022-02-17T12:15:54.000+01:00</created>
</QueryResults>
<QueryResults>
<Id>12</Id>
<created>2022-02-17T12:16:54.000+01:00</created>
</QueryResults>
</Result>
public class ResultModel {
@JsonProperty("Id")
private int id;
@JsonProperty("published")
private Date published;
@JsonProperty("QueryResults")
private List<QueryResult>
// GETTERS and SETTERS
}
public class QueryResult {
@JsonProperty("Id")
private int id;
@JsonProperty("created")
private Date created;
}
当我尝试将字符串读入这样的对象时
try {
com.fasterxml.jackson.dataformat.xml.XmlMapper mapper = new XmlMapper();
ResultModel obj = mapper.readValue(xmlString, ResultModel.class)
} catch (JsonProcessingException e) {
e.printStackTrace();
}
我收到以下错误:
com.fasterxml.jackson.databind.exc.MisMatchedInputException: Cannot construct instance of `a.b.c.d.ResultModel` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ("11")
at [Source: (StringReader); line 1, column 15] (through reference chain: a.b.c.d.ResultModel["QueryResults"]->java.util.ArrayList[0]
我在 class 中使用 @JsonProperty,因为使用 @XmlElement 我还有其他问题。如果我在我的 ResultModel class 中排除字段 QueryResults,那么字符串的解析工作并且看起来正确。我在这里做错了什么?我对解析 Java 中的 XML 字符串非常陌生,所以我希望有一些简单的方法可以解决这个问题...
您需要使用以下 2 个注释:
@JacksonXmlElementWrapper(useWrapping=false)
@JacksonXmlProperty(localName = "QueryResults")
@Data
static class ResultModel {
@JsonProperty("Id")
private int id;
@JsonProperty("published")
private Date published;
@JacksonXmlElementWrapper(useWrapping=false)
@JacksonXmlProperty(localName = "QueryResults")
private List<QueryResult> queryResult;
}
@Data
static class QueryResult {
@JsonProperty("Id")
private int id;
@JsonProperty("created")
private Date created;
}
public static void main(String[] argz) throws JsonProcessingException {
String xml = "<Result>\n"
+ " <published>2022-02-17T14:26:54.000+01:00</published>\n"
+ " <Id>10</Id>\n"
+ " <QueryResults>\n"
+ " <Id>11</Id>\n"
+ " <created>2022-02-17T12:15:54.000+01:00</created>\n"
+ " </QueryResults>\n"
+ " <QueryResults>\n"
+ " <Id>12</Id>\n"
+ " <created>2022-02-17T12:16:54.000+01:00</created>\n"
+ " </QueryResults>\n"
+ "</Result>";
XmlMapper mapper = new XmlMapper();
ResultModel obj = mapper.readValue(xml, ResultModel.class);
System.out.println(obj);
}
控制台:
ResultModel(id=10, published=Thu Feb 17 14:26:54 CET 2022, queryResult=[QueryResult(id=11, created=Thu Feb 17 12:15:54 CET 2022), QueryResult(id=12, created=Thu Feb 17 12:16:54 CET 2022)])
已更新:
Lists and arrays are "wrapped" by default, when using Jackson annotations, but unwrapped when using JAXB annotations (if supported, see below) @JacksonXmlElementWrapper.useWrapping can be set to 'false' to disable wrapping JacksonXmlModule.setDefaultUseWrapper() can be used to specify whether "wrapped" or "unwrapped" setting is the default
https://github.com/FasterXML/jackson-dataformat-xml#known-limitations