Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can JAXB handle multiple "root" elements?

Tags:

jaxb

I have a schema similar to the following...

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="t1" type="t1Type"/>
    <xs:element name="t2" type="t2Type"/>
    <xs:element name="t3" type="t3Type"/>
</xs:schema>

At first I thought this was an invalid schema but all the checks I do online validate it. This means the person supplying the XML can send any (or all) the types listed and still conform to the schema.

How do I go about mapping and unmarshalling all the different possibilities using JAXB?

I have no idea which of them I will be recieving.

like image 257
Jackie Avatar asked Oct 06 '22 07:10

Jackie


1 Answers

You will need to leverage a factory class annotated with @XmlRegistry (usually called ObjectFactory). That class will contain a create method for each possible root element annotated with @XmlElementDecl. See this article I wrote for more details and examples.

like image 109
bdoughan Avatar answered Oct 11 '22 11:10

bdoughan