Java 到 XML 的转换?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4230499/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:25:43  来源:igfitidea点击:

Java to XML conversions?

javaxmlserialization

提问by Rachel

What are different approaches to convert Java Objectsto XML, I know one option is JAXBbut would like to know what are the other approaches/toolsavailable for the same ?

什么是转换不同的方法Java ObjectsXML,我知道有一个选项JAXB,但想知道是什么其他approaches/tools可用于相同?

Note:I do not have further requirements and so I cannot add more meatto 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?

注意:我没有进一步的要求,所以我不能添加更多meat的问题,但在这个时候,如果我能知道有哪些不同的方法可以用于转换,那就太好了Java to XML

Update:Different suggested approaches are:

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

  1. javax.xml.bind.Marshallerand javax.xml.bind.Unmarshaller
  2. XStream
  3. XMLBean
  4. JAXB
  5. Castor
  6. JIBX
  7. Apache Digester
  1. javax.xml.bind.Marshallerjavax.xml.bind.Unmarshaller
  2. 流媒体
  3. XMLBean
  4. JAXB
  5. 蓖麻
  6. JIBX
  7. 阿帕奇消化器

Now among all the suggested approaches what is the BESTapproach to go convert Java Objects to XMLand XML to Java Objects?

现在所有的建议方案中什么是最好的去转换方法Java Objects to XMLXML to Java Objects

回答by bdoughan

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

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

For more information on JAXB check out my blog:

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

UPDATE:

更新:

What is the BEST approach?

最好的方法是什么?

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

这最终取决于您要尝试做什么,我将在下面解释:

Use Case #1 - Starting from an XML Schema

用例 #1 - 从 XML 模式开始

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.

在这个用例中,您有一个 XML 模式并且想要生成一个 Java 模型。此线程中提到的工具并不多见支持此用例。 例如,XStream 为此推荐使用 XMLBeans

Nominees:

提名人:

  • JAXB (all implementations) - Generates POJOs with JAXB annotations.
  • XMLBeans - Generates proprietary classes that include XML binding logic.
  • JAXB(所有实现) - 生成带有 JAXB 注释的 POJO。
  • XMLBeans - 生成包含 XML 绑定逻辑的专有类。

Use Case #2 - Starting from Java Classes (that you can edit)

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

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)。编辑通常涉及添加注释以控制映射。

Nominees:

提名人:

  • Everyone but XMLBeans
  • 除了 XMLBeans 之外的所有人

Use Case #3 - Starting form Java Classes (that you can not edit)

用例 #3 - 启动表单 Java 类(您无法编辑)

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.

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

Nominees:

提名人:

  • 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
  • EclipseLink JAXB (MOXy) - 提供一个外部绑定文件,并且可以通过编程方式应用元数据。
  • Metro JAXB - 可以利用 Annox 或 JAXBIntroductions
  • Castor - 提供外部绑定文件
  • JiBX - 提供外部绑定文件
  • XStream - 可以以编程方式应用元数据

Use Case #4 - Meet-in-the-Middle (Existing classes and schema)

用例 #4 - 中间相遇(现有的类和模式)

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

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

Nominees:

提名人:

  • EclipseLink JAXB (MOXy)
  • EclipseLink JAXB (MOXy)

Use Case #5 - XML Infoset Preservation:

用例 #5 - XML 信息集保存:

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

在这个用例中,您需要保留未映射的内容:评论、处理指令等。

Nominees:

提名人:

  • JAXB (all implementations) - Has the Binderfeature.
  • XMLBeans - The generated object model stores the entire XML infoset.
  • JAXB(所有实现) - 具有Binder功能。
  • XMLBeans - 生成的对象模型存储整个 XML 信息集。

Use Case #6 - Compatibility with JPA

用例 #6 - 与 JPA 的兼容性

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.

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

Nominees:

提名人:

Use Case #7 - Compatibility with XML Web Services (JAX-WS)

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

JAXB is the default binding layer for JAX-WS.

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

Nominees:

提名人:

  • JAXB (implementation depends of the JAX-WS provider)
  • JAXB(实现取决于 JAX-WS 提供者)

Use Case #8 - Compatibility with RESTful Web Services (JAX-RS)

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

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

JAX-RS 提供了一种基于 HTTP 协议的轻量级替代 JAX-WS。请查看以下示例

Nominees:

提名人:

  • JAXB (all implementations) - The default binding layer and easiest to use with JAX-RS.
  • Everything else - You can leverage the concepts of MessageBodyReader/Writer to use other XML tools.
  • JAXB(所有实现) - 默认绑定层,最容易与 JAX-RS 一起使用。
  • 其他一切 - 您可以利用 MessageBodyReader/Writer 的概念来使用其他 XML 工具。

Use Case #9 - Compatibility with Spring

用例 #9 - 与 Spring 的兼容性

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

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

Nominees:

提名人:

  • JAXB (all implementations)
  • Castor
  • XMLBeans
  • JiBX
  • JAXB(所有实现)
  • 蓖麻
  • XMLBeans
  • 吉宝信

Other Things to Consider

其他需要考虑的事情

  • Is the tool still being developed/supported? As funny as this sounds I've seen people recommend tools that haven't beed updated in 5 years. Some of the tools mentioned here haven't released in 2 years.
  • 该工具是否仍在开发/支持中?尽管这听起来很有趣,但我看到人们推荐了 5 年未更新的工具。这里提到的一些工具已经 2 年没有发布了。

My Pick for BEST approach? - 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 可能并不总是最适合特定用例(但它总是很合适),但它是唯一可以用于所有用例的库。这意味着它始终可以完成工作。另一种方法是对不同的任务使用不同的库,为您提供多个库来支持。

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).

我确实领导了 JAXB 实现 EclipseLink MOXy,但 MOXy 最初是作为专有 XML 绑定库 TopLink OXM 的。TopLink 一直了解标准(即 EJB/JPA)的好处,我们实施了 JAXB 1。然后我们(我是代表)成为 JAXB 2 (JSR-222) 的活跃成员。

回答by duffymo

You can always do it yourself with javax.xml.bind.Marshallerand javax.xml.bind.Unmarshallerinterfaces.

您始终可以使用javax.xml.bind.Marshallerjavax.xml.bind.Unmarshaller接口自行完成。

This isn't as crazy as it sounds. It's not that hard to do, and you'll have complete control over and knowledge of what's done.

这并不像听起来那么疯狂。这并不难,您可以完全控制并了解所做的事情。

I'm willing to acknowledge that lots of folks prefer an automated approach. My offering won't be popular.

我愿意承认很多人更喜欢自动化的方法。我的产品不会受欢迎。

I've used XStream in the past and liked it, but when I've used it I didn't have to worry about XML namespaces. I'm told that XStream doesn't deal with them well.

我过去使用过 XStream 并且喜欢它,但是当我使用它时,我不必担心 XML 名称空间。我听说 XStream 不能很好地处理它们。

回答by Martin

Some alternatives include:-

一些替代方案包括:-

However, one significant advantage of going with JAXB is it ships with the Java distrubtion.

但是,使用 JAXB 的一个显着优势是它附带了 Java 发行版。

回答by Mike Baranczak

I know that this isn't what you asked, but have you considered JSON instead of XML? I'm currently working on a project that makes heavy use of JSON (with the Hymansonlibrary) and I'm very happy with it. Hymanson works similarly to JAXB - it even works with JAXB annotations, in case you want to use both.

我知道这不是您问的问题,但是您是否考虑过使用 JSON 而不是 XML?我目前正在开发一个大量使用 JSON(使用Hymanson库)的项目,我对此非常满意。Hymanson 的工作方式与 JAXB 类似 - 它甚至可以与 JAXB 注释一起使用,以防您想同时使用两者。

XMLBeansis also worth looking at.

XMLBeans也值得一看。

回答by ng.

Simple XML Serialization (simpleframework.org)

简单的 XML 序列化 (simpleframework.org)

Another approach is Simpleits far easier to use than JAXB, JiBX or other such tools. You simply annotate class and you can serialize any POJO to XML. Also, its probably the only framework that currently works on all known Java platforms, including Android, Google App Engine, any JDK 1.5+. For more information you can check out the Tutorial

另一种方法是Simple,它比 JAXB、JiBX 或其他此类工具更易于使用。您只需注释类,就可以将任何 POJO 序列化为 XML。此外,它可能是目前唯一适用于所有已知 Java 平台的框架,包括 Android、Google App Engine、任何 JDK 1.5+。有关更多信息,您可以查看教程

回答by SMac

Castor allows generation of Java classes(decent implementations) straight from XSD schema (use case #1 above). Just be sure, in Android 2.1, not to use default android SAXParser. You'll get namespace errors. You do this by defining the parser to be, for example, Xerces (and the you add the required JARS), in core.properties . In android 2.2 it may be ok. Note that if you create an xmlcontext for the unmarsheler with xerces, it still won't work, as the mapping itself would be parsed with android's SAX. It must be done at core (top level properties file) so that even the mapping is parsed by xerces. finally - performance is as slow as you can expect... :( Good luck SM

Castor 允许直接从 XSD 模式(上面的用例 #1)生成 Java 类(体面的实现)。请确保,在 Android 2.1 中,不要使用默认的 android SAXParser。你会得到命名空间错误。例如,您可以通过在 core.properties 中将解析器定义为 Xerces(并添加所需的 JARS)来实现此目的。在 android 2.2 中可能没问题。请注意,如果您使用 xerces 为解组器创建 xmlcontext,它仍然无法工作,因为映射本身将使用 android 的 SAX 进行解析。它必须在核心(顶级属性文件)完成,以便即使映射也能被 xerces 解析。最后 - 性能和你预期的一样慢...... :( 祝你好运 SM

回答by Emil

Apache Digesteris an option.Here is a tutorialfor it.If JSON format is acceptable then you can use google-gson.

Apache Digester是一个选项。这里是它的教程。如果可以接受 JSON 格式,那么您可以使用google-gson

回答by Puce

There are also:

还有:

  • java.beans.XMLEncoder/ -Decoder
  • javax.xml.stream.XMLStreamWriter
  • java.beans.XMLEncoder/ -Decoder
  • javax.xml.stream.XMLStreamWriter

But I recommend to use JAXB if you don't have a good reason to do otherwise.

但是,如果您没有充分的理由不这样做,我建议您使用 JAXB。

回答by fastcodejava

You can look at castorand jibxas well.

您也可以查看castorjibx