使用开源 apis 将 java 对象转换为 xml 的最佳方法是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/736343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the best way to convert a java object to xml with open source apis
提问by Atma
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?
我想知道将 java 对象转换为 xml 的最佳或最广泛使用的 api 是什么。我对这个主题相当陌生。是否有某种 api 调用可以传入一个对象并返回 xml?或者在需要通过拉出对象值手动构建文档时是否更加乏味?
I have been reading about xerces, sax, and jaxb. I would like to continue along this open source route.
我一直在阅读有关 xerces、sax 和 jaxb 的文章。我想继续沿着这条开源路线。
Thanks!
谢谢!
采纳答案by ivan_ivanovich_ivanoff
JAXBis definitely thesolution.
JAXB绝对是解决方案。
Why? Well, it's inside the JDK 6, so you'll never find it unmaintained.
为什么?嗯,它在 JDK 6 中,所以你永远不会发现它没有维护。
It uses Java annotations to declare XML-related properties for classes, methods and fields.
它使用 Java 注释为类、方法和字段声明与 XML 相关的属性。
Note: JAXB also enables you to easily 'unmarshal' XML data (which was previously marshalled from Java object instances) back to object instances.
注意:JAXB 还使您能够轻松地将 XML 数据(以前从 Java 对象实例编组)“解组”回对象实例。
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 tutorialfor getting started.
JAXB 的另一个优点是:它受到其他 Java 相关技术的支持,例如JAX-RS(一种 Java RESTful API,作为Java EE 6 的一部分提供)。JAX-RS 可以即时提供和接收 JAXB 对象,而无需对它们进行编组/解组。您可能想查看Netbeans,它包含对 JAX-RS 的开箱即用支持。阅读本教程以开始使用。
edit:
编辑:
To marshall/unmarshall 'random' (or foreign) Java objects, JAXB offers fairly simple possibility: One can declare an XmlAdapterand 'wrap' existing Java classes to be JAXB-compatible. Usage of such XmlAdapter is done by using the @XmlJavaTypeAdapter-annotation.
为了编组/解组“随机”(或外部)Java 对象,JAXB 提供了相当简单的可能性:可以声明一个XmlAdapter并“包装”现有的 Java 类以与 JAXB 兼容。此类 XmlAdapter 的使用是通过使用@XmlJavaTypeAdapter 注释完成的。
回答by Gary Kephart
You might want to look at XStream: http://x-stream.github.io
你可能想看看 XStream:http: //x-stream.github.io
回答by RealHowTo
What about java.beans.XMLEncoder and java.beans.XMLDecoder ?
java.beans.XMLEncoder 和 java.beans.XMLDecoder 呢?
Example at http://www.rgagnon.com/javadetails/java-0470.html
http://www.rgagnon.com/javadetails/java-0470.html 中的示例
Bye.
再见。
回答by TofuBeer
XMLBeansis another one, similar to JAXB. I haven't looked at JAXB in a while, when I did it was fairly bad compared to XMLBeans, but that was years ago (and I prefer to use things that are in the JDK as opposed to 3rd party ones, but I still use XMLBeans to this day).
XMLBeans是另一种,类似于 JAXB。我有一段时间没有研究 JAXB,当我这样做时,它与 XMLBeans 相比相当糟糕,但那是几年前的事(我更喜欢使用 JDK 中的东西而不是 3rd 方的东西,但我仍然使用XMLBeans 到今天)。
回答by ng.
There are many open source frameworks in this space. However, Simpleas its name suggests, is by far the easiest way to perform serialization. Take a look at the Tutorial. Another feature is that it can perform polymorphic serialization, which means its not as constrained as JAXB for example.
这个领域有很多开源框架。但是,顾名思义,Simple是迄今为止执行序列化的最简单方法。看看教程。另一个特性是它可以执行多态序列化,这意味着它不像 JAXB 那样受限制。
回答by Archimedes Trajano
Available with Java 6 is an API to convert annotated Java Objects to XML. The following code shows how to convert an annotated object to an XML string
Java 6 提供了一个 API,用于将带注释的 Java 对象转换为 XML。以下代码显示了如何将带注释的对象转换为 XML 字符串
final JAXBElement<Type> o = new ObjectFactory().createElement(new Type()); final Marshaller m = JAXBContext.newInstance(Type.class).createMarshaller(); // Do this if you want the result to be more human readable. m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.marshal(o, System.out);
You can further extend this by adding schema validation (note very slow, but useful for debugging)
您可以通过添加模式验证来进一步扩展它(注意很慢,但对调试很有用)
final JAXBElement<Type> o = new ObjectFactory().createElement(new Type()); final Marshaller m = JAXBContext.newInstance(Type.class).createMarshaller(); final Schema schema = SchemaFactory.newInstance( "http://www.w3.org/2001/XMLSchema").newSchema( getClass().getResource("/META-INF/wsdl/schema.xsd")); m.setSchema(schema); m.marshal(o, System.out);
You don't need to do a type conversion to JAXBElement if Type is a defined element. (i.e. has an annotation @XmlRootElement)
如果 Type 是定义的元素,则不需要对 JAXBElement 进行类型转换。(即有一个注释@XmlRootElement)
回答by user1738754
For anyone who decides to use JAXB, please make sure to cache JAXBContext. JAXBContext.newInstance is known to be very expensive. I documented a case where the conversion to xml is used in logging and JAXBContext.newInstance is not cached, it caused performance overhead up to 5seconds when invoked by 20 concurrent users.
对于决定使用 JAXB 的任何人,请确保缓存 JAXBContext。众所周知,JAXBContext.newInstance 非常昂贵。我记录了一个案例,其中在日志记录中使用了到 xml 的转换并且没有缓存 JAXBContext.newInstance,当被 20 个并发用户调用时,它导致性能开销高达 5 秒。
http://app-inf.blogspot.com/2012/10/performance-tuning-logging-right-way.html
http://app-inf.blogspot.com/2012/10/performance-tuning-logging-right-way.html
This is not to discourage anyone using JAXB. Once JAXBContext is cached, the performance is excellent, as discussed in the above link.
这并不是要阻止任何人使用 JAXB。一旦 JAXBContext 被缓存,性能就会非常好,如上述链接中所述。