使用开源api将java对象转换为xml的最佳方式是什么

时间:2022-09-15 20:30:35

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!

谢谢!

7 个解决方案

#1


53  

JAXB is definitely the solution.

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相关属性。

Tutorial 1

教程1

Tutorial 2

教程2

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对象实例中编组的)“unmarshal”回对象实例。

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.

JAXB的另一个优点是:它得到了其他与Java相关的技术的支持,比如JAX-RS(一种Java RESTful API,作为Java EE 6的一部分可以轻松实现)。您可能需要检查Netbeans,它包含对JAX-RS的开箱即用支持。开始阅读本教程。

edit:

编辑:

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.

对于marshall/unmarshall 'random'(或foreign) Java对象,JAXB提供了相当简单的可能性:可以声明XmlAdapter和'wrap'现有Java类是与JAXB兼容的。这种XmlAdapter的使用是通过使用@XmlJavaTypeAdapter-annotation来完成的。

#2


18  

You might want to look at XStream: http://x-stream.github.io

您可能想要查看XStream: http://xstream.github.io。

#3


4  

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是将带注释的Java对象转换为XML的API。下面的代码展示了如何将带注释的对象转换为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)

如果类型是已定义的元素,则不需要对JAXBElement执行类型转换。(即具有注释@XmlRootElement)

#4


3  

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.

再见。

#5


3  

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被认为是非常昂贵的。我记录了在日志记录和JAXBContext中使用xml转换的情况。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被缓存,性能就会很好,如上面的链接所述。

#6


1  

XMLBeans is 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中的东西,而不是第三方的东西,但直到今天我仍然使用XMLBeans)。

#7


1  

There are many open source frameworks in this space. However, Simple as 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.

在这个领域中有许多开放源码框架。然而,就像它的名字所暗示的那样简单,到目前为止是执行序列化的最简单的方式。看一下教程。另一个特性是,它可以执行多态序列化,这意味着它不像JAXB那样受到限制。

#1


53  

JAXB is definitely the solution.

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相关属性。

Tutorial 1

教程1

Tutorial 2

教程2

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对象实例中编组的)“unmarshal”回对象实例。

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.

JAXB的另一个优点是:它得到了其他与Java相关的技术的支持,比如JAX-RS(一种Java RESTful API,作为Java EE 6的一部分可以轻松实现)。您可能需要检查Netbeans,它包含对JAX-RS的开箱即用支持。开始阅读本教程。

edit:

编辑:

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.

对于marshall/unmarshall 'random'(或foreign) Java对象,JAXB提供了相当简单的可能性:可以声明XmlAdapter和'wrap'现有Java类是与JAXB兼容的。这种XmlAdapter的使用是通过使用@XmlJavaTypeAdapter-annotation来完成的。

#2


18  

You might want to look at XStream: http://x-stream.github.io

您可能想要查看XStream: http://xstream.github.io。

#3


4  

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是将带注释的Java对象转换为XML的API。下面的代码展示了如何将带注释的对象转换为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)

如果类型是已定义的元素,则不需要对JAXBElement执行类型转换。(即具有注释@XmlRootElement)

#4


3  

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.

再见。

#5


3  

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被认为是非常昂贵的。我记录了在日志记录和JAXBContext中使用xml转换的情况。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被缓存,性能就会很好,如上面的链接所述。

#6


1  

XMLBeans is 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中的东西,而不是第三方的东西,但直到今天我仍然使用XMLBeans)。

#7


1  

There are many open source frameworks in this space. However, Simple as 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.

在这个领域中有许多开放源码框架。然而,就像它的名字所暗示的那样简单,到目前为止是执行序列化的最简单的方式。看一下教程。另一个特性是,它可以执行多态序列化,这意味着它不像JAXB那样受到限制。