[英]JAXB unmarshaling Ignoring the SOAP Envelope/Header tags
我有一個正在建立的用於訪問Web服務的客戶端。 我正在使用一些JAXB生成的類(Netbeans 6.9)來解組我的xml數據。
嘗試從此Web服務解組InputStream響應時遇到意外元素錯誤,如果將響應保存到文件中,也會遇到相同的意外元素錯誤。
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.w3.org/2003/05/soap-envelope", local:"Envelope"). Expected elements are <{http://www.cmicdataservices.com/}Authentication>,....
將數據保存到文件中后,我可以進入並刪除SOAP標簽(信封,正文,標題),然后毫無問題地進行解組。
我還沒有真正找到一種使拆封忽略這些標簽的方法。 有誰知道該怎么做才能忽略這些標簽?
這是流返回的主要方法和類。
public static void main(String[] args) {
JAXBContext jaxbContext = null;
try {
CMICData cmic = new CMICData();
jaxbContext = JAXBContext.newInstance("cmic.ajrs.com");
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
GetCurrentDataVer1Response response = (GetCurrentDataVer1Response)
unmarshaller.unmarshal( cmic.getCMICIs("GetCurrentDataVer1"));
DatacenterDataVer1 dataSet = response.getGetCurrentDataVer1Result();
List products = dataSet.getAProductBase().getProductBase();
// print some primary keys to show data being processed.
for(Iterator<ProductBase> iter = products.iterator(); iter.hasNext();) {
ProductBase pb = iter.next();
System.out.println(pb.getPkID());
}
} catch (JAXBException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
Netbeans生成的類。
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"getCurrentDataVer1Result"
})
@XmlRootElement(name = "GetCurrentDataVer1Response", namespace = "http://www.cmicdataservices.com/")
public class GetCurrentDataVer1Response {
@XmlElement(name = "GetCurrentDataVer1Result")
protected DatacenterDataVer1 getCurrentDataVer1Result;
/**
* Gets the value of the getCurrentDataVer1Result property.
*
* @return
* possible object is
* {@link DatacenterDataVer1 }
*
*/
public DatacenterDataVer1 getGetCurrentDataVer1Result() {
return getCurrentDataVer1Result;
}
/**
* Sets the value of the getCurrentDataVer1Result property.
*
* @param value
* allowed object is
* {@link DatacenterDataVer1 }
*
*/
public void setGetCurrentDataVer1Result(DatacenterDataVer1 value) {
this.getCurrentDataVer1Result = value;
}
}
有兩種不同的選擇:
選項1
如果您將XML輸入作為InputStream
接收,則可以使用StAX對其進行解析並獲取XMLStreamReader
。 然后,您可以將XMLStreamReader
推進到要解組的本地根元素,並使JAXB解組。
選項#2
您可以使用javax.xml.xpath
庫選擇要與JAXB解組的本地根元素。 有關javax.xml.xpath的示例,請參見:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.