Can JAXB handle multiple "root" elements?

I have a circuit like 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 a wrong scheme, but all the checks that I perform on the Internet check. This means that the person supplying XML can send any (or all) of the types listed and still conforming to the schema.

How do I set up the mapping and unleash all the different features with JAXB?

I have no idea who I will be getting from.

+4
source share
2 answers

You will need to use the factory class annotated with @XmlRegistry (commonly called ObjectFactory ). This class will contain the create method for each possible root element annotated with @XmlElementDecl .

Example

+4
source

I am new to jaxb. I had one doubt regarding jaxb

 @XmlElementDecl(namespace = "http://mtop/nra/xsd/tca/v1", name = "thresholdCrossingAlert", substitutionHeadNamespace = "http://mtop/fmw/xsd/cei/v1", substitutionHeadName = "commoneventInformation") 

This is an annotation in which I get an exception like

 "java.security.PrivilegedActionException: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions" 

ThresholdCrossingAlert.class extends the class EventInformationType.class , and EventInformationType.class extends the class CommonEventInformation.class inturn,

This is any problem with my annotation definition or error in initializing JaxbContext .

0
source

Source: https://habr.com/ru/post/1440051/


All Articles