如何使用JAXB从服务返回的'anyType'创建java对象? [英] How to create java object from 'anyType' returned from service using JAXB?
问题描述
Web服务返回由WSDL定义的对象:
A web service is returning an object defined by the WSDL to be:
<s:complexType mixed="true"><s:sequence><s:any/></s:sequence></s:complexType>
当我打印出这个对象的课程信息时,它会显示为:
When I print out this object's class info, it comes up as:
class com.sun.org.apache.xerces.internal.dom.ElementNSImpl
但我需要将此对象解组为以下类的对象:
But I need to unmarshall this object as an object of the following class:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"info",
"availability",
"rateDetails",
"reservation",
"cancellation",
"error" })
@XmlRootElement(name = "ArnResponse")
public class ArnResponse { }
我知道答案是正确的,因为我知道如何编组这个对象的XML:
I know the response is correct, since I know how to marshall this object's XML:
Marshaller m = jc.createMarshaller();
m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
m.marshal(rootResponse, System.out);
打印出来:
<?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>
如何转动 ElementNSImpl
对象I我看到 ArnResponse
对象我知道它代表什么?
How can I turn the ElementNSImpl
object I'm seeing into the ArnResponse
object I know it represents?
此外,我正在AppEngine上运行,其中文件访问受到限制。
Additionally, I'm running on AppEngine, where file access is restricted.
感谢您的帮助
更新:
我添加了 @XmlAnyElement(lax = true)
注释,如下所示:
I've added the @XmlAnyElement(lax=true)
annotation, like so:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"content"
})
@XmlSeeAlso(ArnResponse.class)
public static class SubmitRequestDocResult {
@XmlMixed
@XmlAnyElement(lax = true)
protected List<Object> content;
但它没有任何区别。
这是否与内容为列表
的事实有关?
Is this something to do with the fact that the content is a List
?
以下是我从服务器取回内容后尝试访问内容的代码:
Here's the code where I'm trying to access the content after getting it back from the server:
List list = rootResponse.getSubmitRequestDocResult().getContent();
for (Object o : list) {
ArnResponse response = (ArnResponse) o;
System.out.println(response);
}
哪个有输出:
2012年1月31日上午10:04:14
com.districthp.core.server.ws.alliance.AllianceApi getRates严重:
com.sun.org .apache.xerces.internal.dom.ElementNSImpl无法强制转换为
com.districthp.core.server.ws.alliance.response.ArnResponse
Jan 31, 2012 10:04:14 AM com.districthp.core.server.ws.alliance.AllianceApi getRates SEVERE: com.sun.org.apache.xerces.internal.dom.ElementNSImpl cannot be cast to com.districthp.core.server.ws.alliance.response.ArnResponse
答案:
axtavt的答案就行了。这工作:
axtavt's answer did the trick. This worked:
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);
推荐答案
您可以将该对象传递给 Unmarshaller.unmarshal(节点)
,它应该能够解组它。
You can pass that object to Unmarshaller.unmarshal(Node)
, it should be able to unmarshal it.
这篇关于如何使用JAXB从服务返回的'anyType'创建java对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!