有人可以帮助我在这里找到我做错了什么。我必须解析以下XML字符串
<?xml version="1.0" encoding="UTF-8"?>
<Person>
<isActive>true</isActive>
<id>A01222262</id>
<name>Bob</name>
<addresses>
<address>
<attributeName>NEWYORK_ADDRESS</attributeName>
<attributeValue>NY 10003</attributeValue>
</address>
<address>
<attributeName>CALIFORNIA_ADDRESS</attributeName>
<attributeValue>CA 92336</attributeValue>
</address>
</addresses>
</Person>
为此,我要映射三个bean,如下所示
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(
name = "",
propOrder = {"isActive", "id", "name", "addresses"}
)
@XmlRootElement(
name = "Person"
)
public class Person {
protected boolean isActive;
@XmlElement(
name = "Id"
)
protected String id;
protected String name;
protected Addresses addresses;
// All Setter and Getter
}
地址类如下所示
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(
name = "Addresses",
propOrder = {"address"}
)
public class Addresses {
@XmlElement(
required = true
)
protected List<Address> address;
//All Setter Getter
}
地址类如下所示
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(
name = "",
propOrder = {"attributeName", "attributeValue"}
)
public class Address {
@XmlElement(
required = true
)
protected String attributeName;
@XmlElement(
required = true
)
protected String attributeValue;
//All Setter Getter
}
这是我在解析时遇到的异常
com.fasterxml.jackson.databind.exc.MismatchedInputException:无法 构造
console.interceptor.Address
的实例(尽管至少 存在一个创建者):不存在用于以下操作的字符串参数构造函数/工厂方法 从[来源:]的字符串值('NEWYORK_ADDRESS')反序列化 (StringReader);行:1,列:173](通过参考链: console.interceptor.Person [“ addresses”]-> console.interceptor.Addresses [“ address”]-> java.util.ArrayList [0])
我正尝试解析如下
XmlMapper xmlMapper=new XmlMapper();
xmlMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
Person p=xmlMapper.readValue(xmlString,Person.class); // xmlSting
我正在使用2.9.8版的jackson-dataformat-xml。
答案 0 :(得分:0)
我尝试了您的课程并且能够像下面一样解析它。我再次检查了版本2.9.8并进行了确认。干杯!
public class StackOverFlowTest {
public static void main(String[] args){
JacksonXmlModule module = new JacksonXmlModule();
module.setDefaultUseWrapper(false);
XmlMapper xmlMapper = new XmlMapper(module);
String xmlString="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Person><isActive>true</isActive><id>A01222262</id><name>Bob</name><addresses><address><attributeName>NEWYORK_ADDRESS</attributeName><attributeValue>NY 10003</attributeValue></address><address><attributeName>CALIFORNIA_ADDRESS</attributeName><attributeValue>CA 92336</attributeValue></address></addresses></Person>";
try {
Person p=xmlMapper.readValue(xmlString,Person.class);
System.out.println(p.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}