Java中的anytype,如何使用JAXB从服务返回的'anyType'创建java对象?

A web service is returning an object defined by the WSDL to be:

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 { }

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

Which prints out:

How can I turn the ElementNSImpl object I'm seeing into the ArnResponse object I know it represents?

Additionally, I'm running on AppEngine, where file access is restricted.

Thanks for any help

Update:

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

But it doesn't make any difference.

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

}

Which has the output:

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

Answer:

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

解决方案

You can pass that object to Unmarshaller.unmarshal(Node), it should be able to unmarshal it.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值