简体   繁体   English

将XML解组为嵌套标签的对象

[英]unmarshal xml to object for a nested tag

I've got some generated classes from a 3rd party provider for their API - specifically Companies House ( http://xmlgw.companieshouse.gov.uk/ ) 我从第三方供应商那里获得了一些针对其API的生成的类-特别是Companies House( http://xmlgw.companieshouse.gov.uk/

I'm having trouble unmarshalling the parts of the response from the API. 我在解组来自API的响应部分时遇到了麻烦。 I can cast the object to a GovTalkMessage object, which contains a Body tag - but the underlying object I get back after unsmarshalling is a ElementNSImpl object instead of the expected pojo. 我可以将对象转换为GovTalkMessage对象,该对象包含一个Body标记-但解组后返回的基础对象是ElementNSImpl对象,而不是预期的pojo。

Example

I create a request like this one: http://xmlgw.companieshouse.gov.uk/examples/companydetails_req.xml 我创建了这样的请求: http : //xmlgw.companieshouse.gov.uk/examples/companydetails_req.xml

And get a response like this one: http://xmlgw.companieshouse.gov.uk/examples/companydetails_reply.xml 并得到这样的响应: http : //xmlgw.companieshouse.gov.uk/examples/companydetails_reply.xml

You can see in the reply that there is: 您可以在回复中看到:

<Body>
<CompanyDetails xmlns="http://xmlgw.companieshouse.gov.uk/v1-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlgw.companieshouse.gov.uk/v1-0/schema/CompanyDetails-v2-1.xsd">
    ...
</Body>

So the contents of the Body (which is a List<Object> in the generated GovTalkMessage pojo) should have a CompanyDetails object in the first element of the list. 因此, GovTalkMessage的内容(在生成的GovTalkMessage pojo中为List<Object> )应该在列表的第一个元素中具有CompanyDetails对象。 I instead have ElementNSImpl . 相反,我有ElementNSImpl

Here's the marshalling code, which works fine - can send it to the API endpoint and comes back with an xml response like the example above: 这是编组代码,可以很好地工作-可以将其发送到API端点并返回xml响应,如上面的示例:

JAXBContext context = JAXBContext.newInstance(GovTalkMessage.class, CompanyDetailsRequest.class, CompanyDetails.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

InputStream in = Request.Post ... //omitted - send to server, get response

Unmarshaller unmarshaller = context.createUnmarshaller();
GovTalkMessage reply = (GovTalkMessage)unmarshaller.unmarshal(in); //ok
Object object = reply.getBody().getAny().get(0); 
//object is ElementNSImpl - should be CompanyDetails

Am I doing something wrong in the unmarshal? 我在海警中做错什么了吗?

Thanks in advance 提前致谢

UPDATE 更新

If I modify the package-info.java to have 如果我将package-info.java修改为

namespace = "http://xmlgw.companieshouse.gov.uk/v1-0/schema"

instead of 代替

namespace = "http://www.govtalk.gov.uk/CM/envelope"

then the unmarshal works. 然后元帅工作。 But I can't get both marshal into GovTalkMessage and unmarshal into CompanyAppointments to work, even if I specify it directy: 但是,即使我直接指定它,我也无法将 GovTalkMessage送到GovTalkMessage并将其GovTalkMessage送到CompanyAppointments中。

JAXBContext payloadContext = JAXBContext.newInstance(CompanyAppointments.class);
Unmarshaller unmarshaller = payloadContext.createUnmarshaller();
unmarshaller.unmarshal((Node)message.getBody().getAny().get(0), CompanyAppointments.class).getValue();

This just gives me a CompanyAppointments with fields full of nulls. 这只是给我一个CompanyAppointments其中的字段充满了空值。

It seems namespace used for generating binding classes and one received in response are conflicted. 似乎用于生成绑定类的名称空间与响应中收到的名称空间发生冲突。

binding class and XML response will be required to point out error here :). 绑定类和XML响应将需要在此处指出错误:)。

I'll suggest you to use package name in place of classes for creating JAXBContext. 我建议您使用包名称代替用于创建JAXBContext的类。

final JAXBContext context = JAXBContext.newInstance("com.something"...);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM