[翻译]  What is a good library for generating arbitrary XML from Java?

[CHINESE]  什么是从Java生成任意XML的好库?


I need to generate a number of XML documents from Java objects. The objects are deep ORM mapped objects, and the XML documents are for a search index (a la Lucene). I want to be able to create a configuration file and feed it a Java object and have it spit out the XML specified in the configuration. Ideally the configuration would consist of a mapping of (possibly deep) properties on the java side to XPath or something very much like it on the XML side.

我需要从Java对象生成许多XML文档。对象是深度ORM映射对象,XML文档用于搜索索引(la Lucene)。我希望能够创建一个配置文件并将其作为Java对象提供,并让它吐出配置中指定的XML。理想情况下,配置将包括Java端(可能很深)属性到XPath的映射或者与XML端非常相似的映射。

JAXB is unsuitable because it wants to the create a one to one mapping from object data to XML nodes. I've looked at JIBX and XStream but neither of them seem to be designed to do what I'm talking about.

JAXB不合适,因为它希望创建从对象数据到XML节点的一对一映射。我看过JIBX和XStream,但它们似乎都没有设计成我正在谈论的内容。

Essentially what I want is Dozer, but designed to create an XML document as its target rather than another Java bean. From my research so far it looks like I'm going to have to write this myself. Can anyone offer a better alternative?

基本上我想要的是Dozer,但设计用于创建XML文档作为其目标而不是另一个Java bean。从我到目前为止的研究看来,我将不得不自己写这个。有人能提供更好的选择吗?

EDIT: The solution must not be predicated on the ability to modify the source Java files. Annotation based systems are comepletely useless to my purposes here. It should be possible to define 'translators' for the individual mappings just as it is in Dozer.

编辑:解决方案不能以修改源Java文件的能力为基础。基于注释的系统对我的目的来说是无用的。应该可以为各个映射定义“翻译器”,就像在Dozer中一样。

It should be noted that I need to be able to specify that a given input field in java might be output in several different places in the XML output, perhaps being transformed in some cases and not in others.

应该注意的是,我需要能够指定java中的给定输入字段可能在XML输出中的几个不同位置输出,可能在某些情况下转换而在其他情况下不转换。

I've already considered doing some sort of straight Java to XML translation of the objects and then performing my task using XSLT, but the problem with that is that these are deep objects with bidirectional connections. Any mapping of the objects to XML would have a difficult time figuring out how deep in object hierarchy to go, even if it could keep track of which objects should be discounted because they'd been seen already.

我已经考虑过对某些对象直接进行Java到XML的转换,然后使用XSLT执行我的任务,但问题是这些是具有双向连接的深层对象。任何对象到XML的映射都很难确定对象层次结构的深度,即使它可以跟踪哪些对象应该被打折,因为它们已经被看到了。

8 个解决方案

#1


4  

i use this: Simple

我用这个:简单

#2


1  

Have you looked at Castor?

你看过Castor吗?

In particular, this quote from their web page makes me think that it might do what you want:

特别是,他们网页上的这句话让我觉得它可能会做你想要的:

Although it is possible to rely on Castor's default behavior to marshal and unmarshal Java objects into an XML document, it might be necessary to have more control over this behavior. For example, if a Java object model already exists, Castor XML Mapping can be used as a bridge between the XML document and that Java object model.

尽管可以依靠Castor的默认行为来将Java对象编组和解组为XML文档,但可能需要对此行为进行更多控制。例如,如果已存在Java对象模型,则Castor XML Mapping可用作XML文档与Java对象模型之间的桥梁。

Castor allows one to specify some of its marshalling/unmarshalling behavior using a mapping file. This file gives explicit information to Castor on how a given XML document and a given set of Java objects relate to each other.

Castor允许使用映射文件指定其某些编组/解组行为。此文件向Castor提供有关给定XML文档和给定Java对象集如何相互关联的明确信息。

Another possibility might be JXM; from their webpage:

另一种可能性可能是JXM;从他们的网页:

Java XML Mapping (JXM) is a tool for writing Java objects to XML and reading them back again. JXM provides a default mapping so that Java objects that follow Java Bean naming conventions can be written to XML by calling a single method. The default mapping can be customized by creating and registering mapping classes with the JXM framework.

Java XML Mapping(JXM)是一种将Java对象编写为XML并再次读取它们的工具。 JXM提供了一个默认映射,以便可以通过调用单个方法将遵循Java Bean命名约定的Java对象写入XML。可以通过使用JXM框架创建和注册映射类来自定义默认映射。

JXM differs from other XML binding tools by not requiring objects to include or inherit from JXM code. No generated code is included in the objects, and they don't need to implement JXM interfaces. Customization is done by writing Java code rather than XML descriptors.

JXM与其他XML绑定工具的不同之处在于不要求对象包含或继承JXM代码。对象中不包含生成的代码,并且它们不需要实现JXM接口。通过编写Java代码而不是XML描述符来完成定制。

#3


1  

Xstream is good

Xstream很好

#4


1  

Take a look at XOM, it's super simple to build XML documents.

看一下XOM,构建XML文档非常简单。

http://www.cafeconleche.org/XOM/

http://www.cafeconleche.org/XOM/

#5


0  

Take a look at Xmappr.

看看Xmappr。

It can be configured via external XML. You can have several mappings that produce different outputs from same input object.

它可以通过外部XML配置。您可以使用多个映射从同一输入对象生成不同的输出。

Also we are willing to help - just ask on the mailing list.

我们也愿意帮忙 - 只需在邮件列表上询问。

#6


0  

We have used Apache Velocity in the past as a means of keeping it simple.

我们过去曾使用Apache Velocity作为保持简单的一种方法。

Simply create a template for the XML, populate a data structure and inject the data structure in the map. It allows you to change the XML at run time if you need to (so long as your data structure has all the fields).

只需为XML创建一个模板,填充数据结构并在地图中注入数据结构。它允许您在运行时更改XML(只要您的数据结构具有所有字段)。

This is a very quick and flexible way to produce a document, it doesn't create a DOM and so is pretty parsimonious with memory.

这是一种非常快速和灵活的生成文档的方法,它不会创建DOM,因此对内存非常简约。

#7


0  

You dont specify which ORM you use, but if by any chance it is hibernate, you can use it to do Java <-> XML mapping as well. The documentation is not as good as for DB mappings, but it is not that hard to use. The big advantage : you have one less dependency, one less framework to learn, and the concepts you have learn for DB mapping till mostly apply to XML mapping.

你没有指定你使用的ORM,但如果它是休眠的话,你也可以用它来做Java < - > XML映射。文档不如DB映射那么好,但它并不难以使用。最大的优势:您可以减少一个依赖项,减少一个学习框架,以及您为数据库映射学习的概念,直到主要应用于XML映射。

see : http://docs.jboss.org/hibernate/core/3.3/reference/en/html/xml.html

请参阅:http://docs.jboss.org/hibernate/core/3.3/reference/en/html/xml.html

#8


0  

The solution you want is EclipseLink MOXy:

您想要的解决方案是EclipseLink MOXy:

MOXy is a JAXB implementation with Extensions

MOXy是带有Extensions的JAXB实现

MOXy has an external configuration file (based on JAXB annotations with extensions):

MOXy有一个外部配置文件(基于带扩展名的JAXB注释):

Has XPath based mapping, for deep mapping:

具有基于XPath的映射,用于深度映射:

Designed to handle ORM mapped objects, including support for bidirectional relationships:

旨在处理ORM映射对象,包括支持双向关系:


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2018 ITdaan.com 粤ICP备14056181号