What is JAXB ?
JAXB stands for Java Architecture for XML Binding, which can be used to convert Java object to XML and XML back to Java object. Basically we will be able to perform the below two operations.
- Marshal – Converting a Java Object into XML
- UnMarshal – Converting XML into Java Object
We will be using marshal()/unmarshal() methods of jaxbMarshaller to convert object to xml and vice-versa.
Required JAXB Annotations
We will be mostly using @XmlRootElement and @XmlElement annotations
@XmlRootElement – Maps a class or an enum type to an XML element.
The @XmlRootElement annotation can be used with the following program elements:
- To a top level class
- To an enum type
@XmlElement – Maps a JavaBean property to a XML element
The @XmlElement annnotation can be used with the following program elements:
- To a JavaBean property
- Toa a non static, non transient field
Marshalling Technique :
During Marshalling we will be converting Java Object into XML format we will be performing the below steps
- Create JAXBContext object
- Create Marshaller object from the context which is created above
- Using the Marshaller object, call the marshal() method
- Pass the Object which has to be converted to the marshal() method
- Finally XML will be created.
UnMarshalling Technique :
During UnMarshalling we will be converting XML back into Java Object format we will be performing the below steps
- Create JAXBContext object
- Create UnMarshaller object from the context which is created above
- Using the UnMarshaller object, call the unmarshal() method
- Pass the XML file which has to be converted as Java Object to the unmarshall() method
- Finally Java Object will be created from XML
We will learn about how to perform Marshalling and UnMarshalling in a detailed manner in the later articles.
Leave a Reply