gpt4 book ai didi

java - 使用 Jackson 解析 XML 时出现 MismatchedInputException

转载 作者:太空宇宙 更新时间:2023-11-04 09:42:03 24 4
gpt4 key购买 nike

谁能帮我找出我在这里做错了什么。我必须解析下面的 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: Cannot construct instance of console.interceptor.Address (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('NEWYORK_ADDRESS') at [Source: (StringReader); line: 1, column: 173] (through reference chain: 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

我使用的是 jackson-dataformat-xml 版本 2.9.8。

最佳答案

我尝试了你的类并能够像下面一样解析它。我也仔细检查了版本 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();
}
}
}

关于java - 使用 Jackson 解析 XML 时出现 MismatchedInputException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55872396/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com