XML到javajaxb
我试图使用JAXB将XML转换为Java,但没有按预期工作。围绕这个问题还有很多其他类似的问题,但我研究的建议解决方案似乎都没有帮助我
下面是我的豆子
@XmlRootElement(name = "ListingResponse", namespace = "http://www.random.com")
@XmlType(propOrder = {"success", "listingId", "description"})
public class ListingResponse {
private String success;
private String listingId;
private String description;
public String getSuccess() {
return success;
}
@XmlElement(name = "Success")
public void setSuccess(String success) {
this.success = success;
}
public String getListingId() {
return listingId;
}
@XmlElement(name = "ListingId")
public void setListingId(String listingId) {
this.listingId = listingId;
}
public String getDescription() {
return description;
}
@XmlElement(name = "Description")
public void setDescription(String description) {
this.description = description;
}
下面是我的解包尝试
ListingResponse response = null;
try {
JAXBContext jaxbContext = JAXBContext.newInstance(ListingResponse.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
response = (ListingResponse) jaxbUnmarshaller.unmarshal(new File("response.xml"));
} catch (JAXBException e) {
e.printStackTrace();
}
最后是我的回应。xml内容
<ListingResponse xmlns="http://www.random.com" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Success>true</Success>
<Description>ListingId 123 created.</Description>
<ListingId>123</ListingId>
</ListingResponse>
- 没有抛出异常李>
- “
response
”不为空李> - 我尝试添加
@XmlAccessorType(XmlAccessType.FIELD / PROPERTY)
{cd3}似乎对这两种方法都没有帮助,但是设置了
但是,响应始终为“空”,没有初始化任何字段
你们能在这里发现问题吗
# 1 楼答案
目前,您只为根元素指定了正确的名称空间限定。您需要使用包级别的
@XmlSchema
注释来映射模型的名称空间限定包裹信息。java
了解更多信息