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 Generated 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 XSD file does not generates the @XMLRootElement in the java file. any one can give a solution for this. I know can generate the root element but this does not work.
jaxb package. An ObjectFactory allows you to programatically construct new instances of the Java representation for XML content. The Java representation of XML content can consist of schema derived interfaces and classes representing the binding of schema type definitions, element declarations and model groups.
In JAXB, marshalling involves parsing an XML content object tree and writing out an XML document that is an accurate representation of the original XML document, and is valid with respect the source schema. JAXB can marshal XML data to XML documents, SAX content handlers, and DOM nodes.
When a top level class or an enum type is annotated with the @XmlRootElement annotation, then its value is represented as XML element in an XML document. This annotation can be used with the following annotations: XmlType , XmlEnum , XmlAccessorType , XmlAccessorOrder .
For global elements corresponding to named complex types an @XmlElementDecl
annotation on the ObjectFactory
class will be generated instead of an @XmlRootElement
annotation on the class. This is because there may be more than one global element corresponding to the same named complex type. This use case could not be met by using @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 the JAXBContext
When creating a JAXBContext
based on a model generated from an XML Schema it should be done on the package name of the generated model. This is so the metadata in the ObjectFactory
class gets processed.
JAXBContext jc = JAXBContext.newInstance("demo5");
Or the generated ObjectFactory
class:
JAXBContext jc = JAXBContext.newInstance(demo5.ObjectFactory.class);
Unmarshalling the Class
When you unmarshal a class in which the root element corresponds to an @XmlElementDecl
annotation you will get an instance of JAXBElement
back.
JAXBElement<PersonType> je = (JAXBElement<PersonType>) unmarshaller.unmarshal(xml);
PersonType pt = je.getValue();
If you want to guard against a JAXBElement
being returned you can always use the JAXBIntrospector
on the result of the unmarshal operation:
PersonType pt = (PersonType) JAXBIntrospector.getValue(unmarshaller.unmarshal(xml));
For More Information
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With