Java中的anytype_如何使用JAXB从服务返回的“ anyType”中创建Java对象?

Web服务正在返回由WSDL定义的对象,该对象为:

当我打印出该对象的类信息时,它显示为:

class com.sun.org.apache.xerces.internal.dom.ElementNSImpl

但我需要将此对象解组为以下类的对象:

@XmlAccessorType(XmlAccessType.FIELD)

@XmlType(name = "", propOrder = {

"info",

"availability",

"rateDetails",

"reservation",

"cancellation",

"error" })

@XmlRootElement(name = "ArnResponse")

public class ArnResponse { }

我知道响应是正确的,因为我知道如何编组此对象的XML:

Marshaller m = jc.createMarshaller();

m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );

m.marshal(rootResponse, System.out);

打印出:

如何将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 content;

但这没有任何区别。

这与内容是的事实有关List吗?

这是从服务器取回内容后要尝试访问的代码:

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

回答:

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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值