For global elements corresponding to the named complex types, the @XmlRootElement annotation in the @XmlRootElement class will be generated instead of the @XmlRootElement annotations of the class. This is due to the fact that there can be more than one global element corresponding to the same complex type. This use case cannot be accomplished with @XmlRootElement .
@XmlRegistry public class ObjectFactory { @XmlElementDecl(name="Person") public JAXBElement<PersonType> createPerson(PersonType personType) { return new JAXBElement<PersonType>(new QName("Person"), PersonType.class, personType); } }
Creating JAXBContext
When creating a JAXBContext based on a model generated from an XML schema, this should be done in the package name of the generated model. This means that metadata in the ObjectFactory class is ObjectFactory processed.
JAXBContext jc = JAXBContext.newInstance("demo5");
Or the generated ObjectFactory class:
JAXBContext jc = JAXBContext.newInstance(demo5.ObjectFactory.class);
Cancel class sorting
When you unmount a class in which the root element matches the @XmlElementDecl annotation, you will get a JAXBElement instance back.
JAXBElement<PersonType> je = (JAXBElement<PersonType>) unmarshaller.unmarshal(xml); PersonType pt = je.getValue();
If you want to protect against the returned JAXBElement , you can always use the JAXBIntrospector as a result of the unmarshal operation:
PersonType pt = (PersonType) JAXBIntrospector.getValue(unmarshaller.unmarshal(xml));
Additional Information
source share