简体   繁体   中英

Converting Java Object to Xml and vice versa

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM