我想将xml转换为java bean。下面是XML字符串。从http://pojo.sodhanalibrary.com/生成的类。执行程序时
OrderByDate o=new OrderByDate();
String xml=o.fetchOrder();
JAXBContext jaxbContext;
jaxbContext = JAXBContext.newInstance( ListOrdersResponse.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(xml);
ListOrdersResponse customer = (ListOrdersResponse) jaxbUnmarshaller.unmarshal(reader);
System.out.println(customer);
错误:
javax.xml.bind.UnmarshalException: unexpected element (uri:"https://mws.amazonservices.com/Orders/2013-09-01", local:"ListOrdersResponse"). Expected elements are <{}ListOrdersResponse>
的xml:
响应: RequestId:4517462f-bcc9-4a45-8636-b5ddb7ec2758 时间戳:2018-04-17T07:20:19.748Z 26Boe17UMTJ / YYuZQbKv1QafEPREmauvizt1MIhPYZY5NIqjMxu3pxmBJMYenxD5B3CvwrdbsefNeGyEmOrLe7q8xqAchQNKtU38YwqRuVIwqcbUqh / LhCJ0wMvlylZkE3RvwDUQBA2PLkbo + RzWuXC2fEO5CgJIJj / kYfkPxDo4dBvlgfuPcB35jNz11rwF7J0jBKMbuVmSwDLw6zPIlbFUu6buhe2mJ / gMIjfO756fpVJGS + vu5pFHMXy6XL5lF750Qfofr7Nm9AiBzmQaanYPFE5UIZ8qsJdJobvDtjbBqDb97MTD8KVC4Vf0cm / vz13 / 9XObD78BiLzBxHwEA0AAEC0dJ7RU11Ds5w1QtjXlOg1Adj + qNDOPK + Zzoe2UHhKIqANvlnPuHxIQFu1qMTHVNyRl6lhkmj1UgSuVUTWxCgKegojxRJfo + JLMPLSjEsqGJJJsu8 + Pnp7zXB4uNZzoMdBRJAKXnMkAgtrAG7zPZjTnVM / HGQ == 2018-04-17T07:18:19Z408-2380372-7405902408-2380372-74059022018-02-02T03 :31:55Z2018-02-02T08:57:30ZShippedAFNAmazon.inStandardrajiv sharmahouse无117扇区1个监狱土地安巴拉CITYHOUSE无117扇区1监狱landAMBALAHARYANA134003ININR483.0010OtherStandardA21TJRUUN4KGV4kbdx30tbk39pc4@marketplace.amazon.inpriyam bhardwajStandardStandardOrder2018-02-02T18:29:59Z2018-02-02T18 :29:59Zfalsefalsefalsefalse4517462f-bcc9-4a45-8636-b5ddb7ec2758
答案 0 :(得分:0)
你应该使用Jackson解析器:
ObjectMapper mapper = new ObjectMapper();
JsonNode actualObj = mapper.readTree(yourString);
答案 1 :(得分:0)
首先,你创建课程的方式是......让我们说有点不正统。 http://pojo.sodhanalibrary.com/显然需要XML或JSON,并基于该XML或JSON生成Java bean。虽然这在某些情况下可能有效,但这绝对不能保证它能够正确地用于符合相同模式的其他 XML / JSON。
为了缩短,您不应该基于XML / JSON示例生成类,您应该从XML Schema(或JSON Schema)生成类。
现在,问你的问题。你的班级显然缺乏名称空间。将以下package-info.java
添加到您拥有ListOrdersResponse
:
@javax.xml.bind.annotation.XmlSchema(
namespace = "https://mws.amazonservices.com/Orders/2013-09-01",
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package whatever.your.package.is;
这假设架构使用elementFormDefault="qualified"
,即本地元素属于同一个https://mws.amazonservices.com/Orders/2013-09-01
命名空间。