Java Architecture for XML Binding (JAXB) - Important tags
JAXB is a standard for mapping POJOS to XML.
It mainly does 2 tasks:
Below are some of the useful tags which can be handy in many scenarios..
@XmlRootElement(name = "Catologue") - Maps the class to a Root element of the generated XML
<?xml version="1.0" encoding="UTF-8"?>
<Catalogue>
</Catalogue>
@XmlType(name = "CatalogueDataType") - Binds/Maps the class to a Schema type
<xs:complexType name="CatalogueDataType">
<xs:sequence>
.......
</xs:sequence>
</xs:complexType>
@XmlType(propOrder = {"name", "products"}) - Order in which the properties of a class appear in XML
<Catalogue>
<name></name>
<products></products>
</Catalogue>
@XmlAccessorType(XmlAccessType.FIELD) - Binds all fields in the class to an XML
@XmlAttribute(name="id", required = true) - Binds the field to an XML attribute
@XmlJavaTypeAdapter(value=ProductAdapter.class) or @XmlJavaTypeAdapter(value=Input.class, type=Output.class)
List<Product> products
- Converts the field to a new type. ProductApter class has to extend the below abstract class and implement those methods
Bound type is the original type. Value type is the type that it converts to.
public abstract class XmlAdapter<ValueType,BoundType>{
public abstract ValueType marshal(BoundType v) throws Exception;
public abstract BoundType unmarshal(ValueType v) throws Exception;
}
@XmlTransient - Ignores a class or method while binding
It mainly does 2 tasks:
- Binding the Java data types to equivalent XSD types.
- By annotating Java class with JAXB annotations, Java objects can be converted to XML documents(Marshalling) and XML docs can be converted back to java objects(Unmarshalling).
Below are some of the useful tags which can be handy in many scenarios..
@XmlRootElement(name = "Catologue") - Maps the class to a Root element of the generated XML
<?xml version="1.0" encoding="UTF-8"?>
<Catalogue>
</Catalogue>
@XmlType(name = "CatalogueDataType") - Binds/Maps the class to a Schema type
<xs:complexType name="CatalogueDataType">
<xs:sequence>
.......
</xs:sequence>
</xs:complexType>
@XmlType(propOrder = {"name", "products"}) - Order in which the properties of a class appear in XML
<Catalogue>
<name></name>
<products></products>
</Catalogue>
@XmlAccessorType(XmlAccessType.FIELD) - Binds all fields in the class to an XML
@XmlAttribute(name="id", required = true) - Binds the field to an XML attribute
@XmlJavaTypeAdapter(value=ProductAdapter.class) or @XmlJavaTypeAdapter(value=Input.class, type=Output.class)
List<Product> products
- Converts the field to a new type. ProductApter class has to extend the below abstract class and implement those methods
Bound type is the original type. Value type is the type that it converts to.
public abstract class XmlAdapter<ValueType,BoundType>{
public abstract ValueType marshal(BoundType v) throws Exception;
public abstract BoundType unmarshal(ValueType v) throws Exception;
}
@XmlTransient - Ignores a class or method while binding
Comments
Post a Comment