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!!

2
  • 1
    The term is serialization and deserialization - look them up. Commented Jan 7, 2015 at 12:23
  • possible duplicate of XML serialization in Java? Commented Jan 7, 2015 at 12:24

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.