简体   繁体   English

从2个xsd文件中有2个XmlRootElements时进行编组/解组

[英]Marshal/unmarshal when there are 2 XmlRootElements from 2 xsd files

Something tells me I'm performing a Java horror show with xsd to class generation via JAXB... Hopefully someone can tell me what! 有人告诉我,我正在使用xsd执行Java恐怖表演,以通过JAXB生成类……希望有人可以告诉我什么!

I've been provided with several related XSD's, which I need to be able to interact with an API. 我已经获得了几个相关的XSD,它们需要能够与API交互。 Let's say there is A.xsd and B.xsd. 假设有A.xsd和B.xsd。 A.xsd defines a body element that can essentially contain a list of any type, including B objects: A.xsd定义了一个body元素,该元素本质上可以包含任何类型的列表,包括B个对象:

<xsd:element name="Body" minOccurs="0">
<xsd:complexType>
<xsd:sequence>
<xsd:any namespace="##any" processContents="lax" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:anyAttribute namespace="##any"/>
</xsd:complexType>
</xsd:element>

So I generate the classes for A.xsd, whose root element is tagged like this: 因此,我为A.xsd生成了类,其根元素被标记为:

@XmlRootElement(name = "A", namespace = "http://www.example.com/a")
public class A {

And I create package-info.java in the same package to have that namespace so I can marshal it: 然后在同一个包中创建package-info.java来拥有该名称空间,以便将其编组:

@XmlSchema(namespace = "http://www.example.com/a", elementFormDefault = XmlNsForm.QUALIFIED)
package com.mypackage;

Now I generate the classes for B.xsd into the same package . 现在,我将B.xsd的类生成到同一包中 (I renamed the A's ObjectFactory to something else because IntelliJ doesn't append the factory). (我将A的ObjectFactory重命名为其他名称,因为IntelliJ不追加工厂)。

( EDIT : I had also created them in different packages but then I get marshalling issues because the request xml needs both A and B.) 编辑 :我也在不同的程序包中创建了它们,但随后出现编组问题,因为请求xml同时需要A和B。)

(Sub-question - is this the way you would ordinarily do it?) (子问题-您通常会采用这种方式吗?)

So the B class now has: 因此,B类现在具有:

@XmlRootElement(name = "B")
public class B {

With this I can now create an A, and marshal it. 有了这个,我现在可以创建一个A并将其编组。 I don't get nasty ns2's when I embed B's in the body thanks to the package-info.java (a la Java: Marshalling Object -- Removing extra ns2 annotation in xml ). 多亏了package-info.java (我将Java编组对象-删除xml中多余的ns2注释 ),将B嵌入到主体中时,我并没有得到讨厌的ns2。

The response message I get from the API also gives me an A, which I can unmarshal without an issue. 我从API获得的响应消息也给了我一个A,我可以毫无问题地将其编组。 BUT the underlying object in the Body, which should be a B will not get unmarshalled - it is a Node object, so I tried: 但是 Body中的基础对象(应该是B不会被解组-它是Node对象,所以我尝试了:

JAXBContext specialContext = JAXBContext.newInstance(B.class);
Object companyAppt = specialContext.createUnmarshaller().unmarshal((Node)reply.getBody().getAny().get(0));

But I get this exception: 但是我得到这个异常:

javax.xml.bind.UnmarshalException: unexpected element (uri:"/some/uri/location", local:"B"). Expected elements are <{http://www.example.com/a}B>

Therefore I think the answer lies in that package-info.java but I just don't know how to manipulate it. 因此,我认为答案就在那个package-info.java中,但我只是不知道如何操作它。

Thanks in advance. 提前致谢。

EDIT 编辑

Here's the xsd's i'm working with: 这是我正在使用的xsd:

"A" in my example = http://xmlgw.companieshouse.gov.uk/v1-0/schema/Egov_ch-v2-0.xsd 在我的示例中,“ A” = http://xmlgw.companieshouse.gov.uk/v1-0/schema/Egov_ch-v2-0.xsd

"B" in my exmaple = http://xmlgw.companieshouse.gov.uk/v1-0/schema/CompanyDetails-v2-1.xsd 我的例子中的“ B” = http://xmlgw.companieshouse.gov.uk/v1-0/schema/CompanyDetails-v2-1.xsd

Other dependant xds: http://xmlgw.companieshouse.gov.uk/v1-0/schema/chbase-v2-1.xsd 其他相关的xds: http ://xmlgw.companieshouse.gov.uk/v1-0/schema/chbase-v2-1.xsd

Why dont you create the Jaxb classes using xjc where you can pass multiple xsds and use a bindings file if there is conflict. 为什么不使用xjc创建Jaxb类,如果有冲突,您可以在其中传递多个xsds并使用绑定文件。

xjc -d out A.xsd B.xsd xjc -d out A.xsd B.xsd

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

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