简体   繁体   English

将Java对象转换为Xml,反之亦然

[英]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. 假设我有一个Employee类,它引用了另一个具有自己属性的类,例如address。 So please let me know how to convert that into xml file and also the vice versa of it. 因此,请让我知道如何将其转换为xml文件,反之亦然。

Thanks!! 谢谢!!

JAXB - It will help you to convert Object to XML and XML to Object. JAXB它将帮助您将Object转换为XML以及将XML转换为Object。 https://jaxb.java.net/ https://jaxb.java.net/

The easiest way is to use the JAXB class that is part of the standard library. 最简单的方法是使用标准库中的JAXB类。

Java Object => XML Java对象=> XML

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

XML => Java object XML => Java对象

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. 您可以使用类及其字段上的注释来自定义序列化/反序列化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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