Một dịch vụ web được trả lại một đối tượng được định nghĩa bởi WSDL là:Làm thế nào để tạo đối tượng java từ 'anyType' được trả về từ dịch vụ bằng JAXB?
<s:complexType mixed="true"><s:sequence><s:any/></s:sequence></s:complexType>
Khi tôi in ra thông tin lớp của đối tượng này, nó đi kèm như:
class com.sun.org.apache.xerces.internal.dom.ElementNSImpl
Nhưng tôi cần phải unmarshall này đối tượng như một đối tượng của lớp sau đây:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"info",
"availability",
"rateDetails",
"reservation",
"cancellation",
"error" })
@XmlRootElement(name = "ArnResponse")
public class ArnResponse { }
tôi biết câu trả lời là đúng, vì tôi biết làm thế nào để marshall XML của đối tượng này:
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(rootResponse, System.out);
nào in ra:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:SubmitRequestDocResponse xmlns:ns2="http://tripauthority.com/hotel">
<ns2:SubmitRequestDocResult>
<!-- below is the object I'm trying to unmarshall -->
<ArnResponse>
<Info />
<Availability>
<!-- etc-->
</Availability>
</ArnResponse>
</ns2:SubmitRequestDocResult>
</ns2:SubmitRequestDocResponse>
Làm thế nào tôi có thể biến các đối tượng ElementNSImpl
tôi nhìn thấy vào ArnResponse
đối tượng Tôi biết nó đại diện?
Ngoài ra, tôi đang chạy trên AppEngine, nơi quyền truy cập tệp bị hạn chế.
Nhờ sự giúp đỡ
Cập nhật:
Tôi đã thêm @XmlAnyElement(lax=true)
chú thích, như vậy:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"content"
})
@XmlSeeAlso(ArnResponse.class)
public static class SubmitRequestDocResult {
@XmlMixed
@XmlAnyElement(lax = true)
protected List<Object> content;
Nhưng nó không thực hiện bất kỳ sự khác biệt.
Đây có phải là điều cần làm với nội dung là List
không?
Dưới đây là đoạn code mà tôi đang cố gắng để truy cập vào các nội dung sau khi nó trở lại từ máy chủ:
List list = rootResponse.getSubmitRequestDocResult().getContent();
for (Object o : list) {
ArnResponse response = (ArnResponse) o;
System.out.println(response);
}
Trong đó có các đầu ra:
ngày 31 tháng 1 năm 2012 10:04: 14 AM com.districthp.core.server.ws.alliance.AllianceApi getRates SEVERE: com.sun.org.apache.xerces.internal.dom.ElementNSImpl không thể truyền tới com.districthp.core.server.ws. alliance.response.ArnResponse
trả lời: Câu trả lời
axtavt đã làm các trick. Điều này làm việc:
Object content = ((List)result.getContent()).get(0);
JAXBContext context = JAXBContext.newInstance(ArnResponse.class);
Unmarshaller um = context.createUnmarshaller();
ArnResponse response = (ArnResponse)um.unmarshal((Node)content);
System.out.println("response: " + response);
Tôi có bản sao của tất cả các lược đồ cho các đối tượng phản hồi và WSDL, nếu nó giúp biết điều đó. – Cuga
Lưu ý rằng 'Node' được sử dụng ở đây là' org.w3c.dom.Node'. –