【发布时间】:2026-01-23 07:15:01
【问题描述】:
这是我收到的 xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:Header/>
<soap-env:Body RequestId="1503948112779" Transaction="HotelResNotifRS">
<OTA_HotelResNotifRS xmlns="http://www.opentravel.org/OTA/2003/05" TimeStamp="2017-08-28T19:21:54+00:00" Version="1.003">
<Errors>
<Error Code="450" Language="en-us" ShortText="Unable to process " Status="NotProcessed" Type="3">Discrepancy between ResGuests and GuestCounts</Error>
</Errors>
</OTA_HotelResNotifRS>
</soap-env:Body>
</soap-env:Envelope>
这会被伪解组为 OTAHotelResNotifRS 对象,您可以在该对象上获取 .getErrors()。问题是没有与该位相关联的类型,因此它以 ElementNSImpl 形式的 Object 形式返回。我不控制 OTAHotelResNotifRS 对象,所以我最好的办法是将 .getErrors() 对象解组为我自己的 pojo。这是我的尝试。
@XmlRootElement(name = "Errors")
@XmlAccessorType(XmlAccessType.FIELD)
public class CustomErrorsType {
@XmlAnyElement(lax = true)
private String[] errors;
public String[] getErrors() {
return errors;
}
}
这是用于尝试将其解组到我的 CustomErrorsType 对象中的代码
Object errorsType = otaHotelResNotifRS.getErrors();
JAXBContext context = JAXBContext.newInstance(CustomErrorsType.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
CustomErrorsType customErrorsType = (CustomErrorsType)unmarshaller.unmarshal((Node)errorsType);
调用unmarshal时抛出如下异常
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.opentravel.org/OTA/2003/05", local:"Errors"). Expected elements are <{}Errors>
有什么想法吗?在 xml 解组方面我很弱。
【问题讨论】:
标签: java xml exception unmarshalling javax