Pages

Monday, August 31, 2015

JAXB

It is java in-build function which can help to convert bean to XML and XML to java bean.
It wouldn't expect the external jar for java 1.6 or above.
It is the XML format of parent and child tag.
for example,


<name>siva</name>
<age>age10</age>
<mark>
      <english> 99</english>
       <tamil>88</tamil>
</mark>

JAVAXML to OBJECT:-


try {
            JAXBContext context = JAXBContext.newInstance(Student.class);
            Unmarshaller un = context.createUnmarshaller();
            Student std= (Student) un.unmarshal(new File(FILE_NAME));
            System.out.println(emp);
        } catch (JAXBException e) {
            e.printStackTrace();
        }


OBJECT to JAVAXML:-

 try {
            JAXBContext context = JAXBContext.newInstance(Student.class);
            Marshaller m = context.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            m.marshal(emp, new File(FILE_NAME));
        } catch (JAXBException e) {
            e.printStackTrace();
        }

No comments:

Post a Comment