2

Suppose I have an Employee Class which has a reference of another class say address which has its own attributes. So please let me know how to convert that into xml file and also the vice versa of it.

Thanks!!

Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
Sharique
  • 781
  • 5
  • 12
  • 33

2 Answers2

5

JAXB - It will help you to convert Object to XML and XML to Object. https://jaxb.java.net/

atish shimpi
  • 4,873
  • 2
  • 32
  • 50
1

The easiest way is to use the JAXB class that is part of the standard library.

Java Object => XML

JAXB.marshal(yourObject, new File("obj.xml"));

XML => Java object

YourClass yourObject = JAXB.unmarshal(new File("obj.xml"), YourClass.class);

You can customize the serialization/deserialization with annotation on the class and on its fields.

icza
  • 389,944
  • 63
  • 907
  • 827