Java到XML的转换? [英] Java to XML conversions?

查看:102
本文介绍了Java到XML的转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java对象转换为 XML 有哪些不同的方法,我知道一个选项是 JAXB 但是想知道同样的其他方法/工具是什么?

What are different approaches to convert Java Objects to XML, I know one option is JAXB but would like to know what are the other approaches/tools available for the same ?

注意: 我没有进一步的要求,因此我无法在问题中添加更多 meat 在这个时间点,如果我能够了解哪些不同的方法可用于将 Java转换为XML ,那将会非常棒?

Note: I do not have further requirements and so I cannot add more meat to the question but at this point of time it would be really great if I can get an idea of what different approaches are available for converting Java to XML ?

更新: 不同的建议方法是:

Update: Different suggested approaches are:


  1. < a href =http://download.oracle.com/javase/6/docs/api/javax/xml/bind/Marshaller.html\"rel =noreferrer> javax.xml.bind.Marshaller 和 javax.xml.bind.Unmarshaller

  2. XStream

  3. XMLBean

  4. JAXB

  5. Castor

  6. JIBX

  7. Apache Digester

  1. javax.xml.bind.Marshaller and javax.xml.bind.Unmarshaller
  2. XStream
  3. XMLBean
  4. JAXB
  5. Castor
  6. JIBX
  7. Apache Digester

现在所有建议的方法中, 最佳 方法转换为 Java对象到XML XML到Java对象

Now among all the suggested approaches what is the BEST approach to go convert Java Objects to XML and XML to Java Objects ?

推荐答案

JAXB是将Java对象转换为XML的标准和最佳方法。有几种开源实现可用:

JAXB is the standard and the best approach for coverting Java objects to XML. There are several open source implementations available:

  • EclipseLink MOXy (I'm the tech lead)
  • Metro (The reference implementation, included in Java SE 6)
  • JaxMe

有关JAXB的更多信息,请查看我的博客:

For more information on JAXB check out my blog:

  • http://bdoughan.blogspot.com

更新:

什么是最好的方法?

这最终取决于你想要做什么,我将在下面解释:

This ultimately depends on what you are trying to do, I'll explain below:

用例#1 - 从XML模式开始

在这个用例中,您有一个XML模式而你想要发挖掘Java模型。此线程中提到的工具中没有多少支持此用例。例如, XStream建议使用XMLBeans

In this use case you have an XML schema and you want to generate a Java model. Not many of the tools mentioned in this thread support this use case. XStream for example recommends XMLBeans for this.

被提名人:


  • JAXB(所有实现) - 使用JAXB注释生成POJO。

  • XMLBeans - 生成包含XML绑定逻辑的专有类。

用例#2 - 从Java类开始(你可以编辑)

在这个用例中你有更多的选择(只消除了XMLBeans)。编辑通常需要添加注释来控制映射。

In this use case you have much more selection (only XMLBeans is eliminated). The edits normally involve the addition of annotations to control the mapping.

被提名者:


  • 除XMLBeans之外的所有人

用例#3 - 从Java类开始(无法编辑)

在此用例中,您没有修改模型类的源代码。这需要使用代码的XML文件从外部提供元数据。

In this use case you do not have the source to modify the model classes. This requires the metadata to be supplied externally either with an XML file of by code.

被提名者:


  • EclipseLink JAXB(MOXy) - 提供外部绑定文件,元数据可以通过编程方式应用。

  • Metro JAXB - 可以利用Annox或JAXBIntroductions

  • Castor - 提供外部绑定文件

  • JiBX - 提供外部绑定文件

  • XStream - 可以通过编程方式应用元数据

  • EclipseLink JAXB (MOXy) - Offers an external binding file, and metadata can be applied programmatically.
  • Metro JAXB - Can leverage Annox or JAXBIntroductions
  • Castor - Offers an external binding file
  • JiBX - Offers an external binding file
  • XStream - Metadata can be applied programmatically

用例#4 - 中间会面(现有类和架构)

In在这个用例中,您需要映射到现有XML模式的现有类。 EclipseLink MOXy及其基于XPath的映射是我知道的唯一可以处理这个用例的工具

In this use case you have existing classes that you need to map to an existing XML schema. EclipseLink MOXy with its XPath based mapping is the only tool I'm aware of that can handle this use case

被提名者:


  • EclipseLink JAXB(MOXy)

用例#5 - XML Infoset保存:

在此用例中,您需要保留未映射的内容:评论,处理说明等。

In this use case you need to preserve the unmapped content: comments, processing instructions etc.

被提名人:


  • JAXB(所有实现) - 具有 Binder 功能。

  • XMLBeans - 生成的对象模型存储整个XML信息集。

  • JAXB (all implementations) - Has the Binder feature.
  • XMLBeans - The generated object model stores the entire XML infoset.

使用案例#6 - 与JPA的兼容性

JPA是Java持久性的Java标准。 JPA有许多概念:复合键,双向关系,延迟加载等,这些都很难与XML绑定解决方案一起使用。例如,任何只通过字段与对象交互的XML工具通常会出现延迟加载属性的问题。

JPA is the Java standard for Java persistence. JPA has many concepts: composite keys, bidirectional relationships, lazy loading, etc that can be hard to use with an XML binding solution. For example any XML tool that only interacts with objects via the field will usually have problems with lazy loading properties.

被提名者:

  • EclipseLink JAXB (MOXy) - Was built with JPA in mind.

用例#7 - 与XML Web服务(JAX-WS)的兼容性

JAXB是JAX-WS的默认绑定层。

JAXB is the default binding layer for JAX-WS.

被提名者:


  • JAXB(实现取决于JAX-WS提供者)

用例#8 - 与RESTful Web服务的兼容性(JAX-RS)

JAX-RS是JAX的轻量级替代品-WS基于HTTP协议。请查看以下内容,了解示例

JAX-RS offers a light-weight alternative to JAX-WS based on the HTTP protocol. Check out the following for an example.

被提名者:


  • JAXB(所有实现) - 默认绑定层,最容易与JAX-RS一起使用。

  • 其他 - 您可以利用MessageBodyReader / Writer的概念来使用其他XML工具。

用例#9 - 与Spring的兼容性

Spring有一些内置支持集成使用XML绑定工具,请查看以下链接以获取更多信息:

Spring has some built in support for integrating with XML binding tools, check out the following link for more information:

  • http://static.springsource.org/spring-ws/docs/0.9.1/reference/oxm.html

被提名人:


  • JAXB(所有实现)

  • Castor

  • XMLBeans

  • JiBX

  • JAXB (all implementations)
  • Castor
  • XMLBeans
  • JiBX

需要考虑的其他事项


  • 该工具是否仍在开发/支持?听起来很有趣,我看到人们推荐的工具在5年内没有更新。这里提到的一些工具在2年内没有发布。

我选择BEST方法? - JAXB

看看上面的类别,JAXB可能并不总是最适合特定的用例(但它总是很合适),但它是唯一可用于所有用例的库。这意味着它总能完成这项工作。另一种方法是为不同的任务使用不同的库,为您提供多个库支持。

Looking at the above categories, JAXB may not always be the best fit for a particular use case (but it is always a good fit), but it is the only library that can be used for all the use cases. This means it can always do the job. The alternative is to use different libraries for different tasks giving you multiple libraries to support.

我确实领导了一个JAXB实现EclipseLink MOXy,但MOXy作为专有XML开始了它的生命绑定库TopLink OXM。 TopLink始终了解标准的好处(即EJB / JPA),我们实现了JAXB 1.然后我们(我是代表性的)成为JAXB 2(JSR-222)的活跃成员。

I do lead a JAXB implementation EclipseLink MOXy, but MOXy began its life as a proprietary XML binding library TopLink OXM. TopLink has always understood the benefit of standards (i.e. EJB/JPA), and we implemented JAXB 1. Then we (I am the represetative) became active members on JAXB 2 (JSR-222).

这篇关于Java到XML的转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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