JAX-B Marshalling: Java Object into XML document

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.

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

  • @XmlRootElement specifies the root element for the xml document.

  • @XmlAttribute specifies the attribute for the root element.

  • @XmlElement specifies the sub element for the root element.

Create Employee POJO class

SOURCE:Employee.java
SOURCE:ObjectToXML.java
OUTPUT:


    25
    IT
    Jhon klazck


SHARE

    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment