JAX-B API provides Marshaller interface, It is used to marshal(write) the object into xml document.
In this example, we are going to discuss about conversion of the object into xml using JAX-B.
SOURCE:
OUTPUT:
Steps to convert java object into XML document.
- Create POJO or bind the schema and generate the classes.
- Create the JAXBContext object.
- Create the Marshaller objects.
- Create the content tree by using set methods.
- Call the marshal method.
Before we looking in to example. First we need to know about annotations which requires to define the xml doc information in POJO.Some of the annotations are listed below
@XmlRootElementspecifies the root element for the xml document.@XmlAttributespecifies the attribute for the root element.@XmlElementspecifies the sub element for the root element.
Create Employee POJO class
SOURCE:Employee.java
SOURCE:
ObjectToXML.java
OUTPUT:
25 IT Jhon klazck
0 comments :
Post a Comment