简体   繁体   中英

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!

I've been provided with several related XSD's, which I need to be able to interact with an API. Let's say there is A.xsd and B.xsd. A.xsd defines a body element that can essentially contain a list of any type, including B objects:

<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:

@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:

@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 . (I renamed the A's ObjectFactory to something else because IntelliJ doesn't append the factory).

( EDIT : I had also created them in different packages but then I get marshalling issues because the request xml needs both A and B.)

(Sub-question - is this the way you would ordinarily do it?)

So the B class now has:

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

With this I can now create an A, and marshal it. 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 ).

The response message I get from the API also gives me an A, which I can unmarshal without an issue. BUT the underlying object in the Body, which should be a B will not get unmarshalled - it is a Node object, so I tried:

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.

Thanks in advance.

EDIT

Here's the xsd's i'm working with:

"A" in my example = 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

Other dependant 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 -d out A.xsd B.xsd

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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