Does not generate @XMLRootElement jaxb

This is my .xsd file

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Person" type="PersonType"/> <xs:complexType name="PersonType"> <xs:sequence> <xs:element name="Name" type="xs:string"/> <xs:element name="Address" type="AddressType" minOccurs="1" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> <xs:complexType name="AddressType"> <xs:sequence> <xs:element name="Number" type="xs:unsignedInt"/> <xs:element name="Street" type="xs:string"/> </xs:sequence> </xs:complexType> 

using this xsd file. I created this class:

 package demo5; import java.util.ArrayList; import java.util.List; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlType; @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "PersonType", propOrder = { "name", "address" }) public class PersonType { @XmlElement(name = "Name", required = true) protected String name; @XmlElement(name = "Address", required = true) protected List<AddressType> address; public String getName() { return name; } public void setName(String value) { this.name = value; } public List<AddressType> getAddress() { if (address == null) { address = new ArrayList<AddressType>(); } return this.address; } } 

but the XSD file does not generate @XMLRootElement in the java file. anyone can give a solution for this. I know that it can generate a root element, but this does not work.

+6
source share
2 answers

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

+6
source

@XMLRootElement will only be generated for anonymous top element types, not for top level types.

+1
source

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


All Articles