使用开源 apis 将 java 对象转换为 xml 的最佳方法是什么 [英] What is the best way to convert a java object to xml with open source apis

查看:33
本文介绍了使用开源 apis 将 java 对象转换为 xml 的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道将 java 对象转换为 xml 的最佳或最广泛使用的 api 是什么.我对这个主题相当陌生.是否有某种 api 调用可以传入一个对象并返回 xml?或者,在需要通过拉出对象值手动构建文档的情况下,是否更加繁琐?

I was wondering what the best or most widely used apis are to convert a java object to xml. I'm fairly new on the subject. Is there some sort of api call you can make to pass in an object and return xml? Or is it much more tedious where as you need to construct the document manually by pulling out object values?

我一直在阅读有关 xerces、sax 和 jaxb 的文章.我想沿着这条开源路线继续前进.

I have been reading about xerces, sax, and jaxb. I would like to continue along this open source route.

谢谢!

推荐答案

JAXB 绝对是 解决方案.

为什么?嗯,它在 JDK 6 中,所以你永远不会发现它没有维护.

Why? Well, it's inside the JDK 6, so you'll never find it unmaintained.

它使用 Java 注释为类、方法和字段声明与 XML 相关的属性.

It uses Java annotations to declare XML-related properties for classes, methods and fields.

教程 1

教程 2

注意:JAXB 还使您能够轻松地解组"XML 数据(之前从 Java 对象实例编组)返回对象实例.

Note: JAXB also enables you to easily 'unmarshal' XML data (which was previously marshalled from Java object instances) back to object instances.

JAXB 的另一个优点是:它被其他 Java 相关的技术,例如 JAX-RS(Java RESTful API,可用于作为 Java EE 6 的一部分).JAX-RS 可以服务和接收 JAXB对象动态,无需编组/解组它们.您可能想查看 Netbeans,其中包含对 JAX-RS 的开箱即用支持.阅读本教程以开始使用.

One more great thing about JAXB is: It is supported by other Java-related technologies, such as JAX-RS (a Java RESTful API, which is availible as part of Java EE 6). JAX-RS can serve and receive JAXB objects on the fly, without the need of marshalling/unmarshalling them. You might want to check out Netbeans, which contains out-of-the-box support for JAX-RS. Read this tutorial for getting started.

要编组/解组随机"(或外部)Java 对象,JAXB提供了相当简单的可能性:可以声明一个 XmlAdapter并将现有的 Java 类包装"为与 JAXB 兼容.此类 XmlAdapter 的使用是通过使用 @XmlJavaTypeAdapter 注释完成的.

To marshall/unmarshall 'random' (or foreign) Java objects, JAXB offers fairly simple possibility: One can declare an XmlAdapter and 'wrap' existing Java classes to be JAXB-compatible. Usage of such XmlAdapter is done by using the @XmlJavaTypeAdapter-annotation.

这篇关于使用开源 apis 将 java 对象转换为 xml 的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆