Cug*_*uga 18 java serialization jaxb unmarshalling
Web服务返回由WSDL定义的对象:
<s:complexType mixed="true"><s:sequence><s:any/></s:sequence></s:complexType>
Run Code Online (Sandbox Code Playgroud)
当我打印出这个对象的类信息时,它出现为:
class com.sun.org.apache.xerces.internal.dom.ElementNSImpl
Run Code Online (Sandbox Code Playgroud)
但我需要将此对象解组为以下类的对象:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"info",
"availability",
"rateDetails",
"reservation",
"cancellation",
"error" })
@XmlRootElement(name = "ArnResponse")
public class ArnResponse { }
Run Code Online (Sandbox Code Playgroud)
我知道响应是正确的,因为我知道如何编组这个对象的XML:
Marshaller m = jc.createMarshaller();
m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
m.marshal(rootResponse, System.out);
Run Code Online (Sandbox Code Playgroud)
打印出来:
<?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>
Run Code Online (Sandbox Code Playgroud)
如何将我所ElementNSImpl看到的ArnResponse物体转变为我所知道的物体?
此外,我正在AppEngine上运行,其中文件访问受到限制.
谢谢你的帮助
更新:
我添加了@XmlAnyElement(lax=true)注释,如下所示:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"content"
})
@XmlSeeAlso(ArnResponse.class)
public static class SubmitRequestDocResult {
@XmlMixed
@XmlAnyElement(lax = true)
protected List<Object> content;
Run Code Online (Sandbox Code Playgroud)
但它没有任何区别.
这与内容是List什么有关吗?
这是我从服务器获取内容后尝试访问内容的代码:
List list = rootResponse.getSubmitRequestDocResult().getContent();
for (Object o : list) {
ArnResponse response = (ArnResponse) o;
System.out.println(response);
}
Run Code Online (Sandbox Code Playgroud)
哪个有输出:
2012年1月31日上午10:04:14 com.districthp.core.server.ws.alliance.AllianceApi getRates SEVERE:com.sun.org.apache.xerces.internal.dom.ElementNSImpl无法强制转换为com.districthp.core .server.ws.alliance.response.ArnResponse
回答:
axtavt的回答就是这个伎俩.这有效:
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);
Run Code Online (Sandbox Code Playgroud)
您可以使用@XmlAnyElement(lax=true)。这会将具有已知根元素(@XmlRootElement或@XmlElementDecl)的XML转换为域对象。有关示例,请参见:
| 归档时间: |
|
| 查看次数: |
30141 次 |
| 最近记录: |