解組XML時遇到以下是XML我收到在Java中
<?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形式的對象返回。我不控制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);
調用解組
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.opentravel.org/OTA/2003/05", local:"Errors"). Expected elements are <{}Errors>
有什麼想法時,它拋出以下異常?對於xml解組來說,我非常虛弱。
這樣做。謝謝! – Aubanis