[翻译]  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.


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.


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.


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.


8 个解决方案



i use this: Simple




Have you looked at 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.


Another possibility might be JXM; from their webpage:


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.




Xstream is good




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






Take a look at Xmappr.


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


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

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



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


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.




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




The solution you want is EclipseLink MOXy:

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

MOXy is a JAXB implementation with Extensions


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


Has XPath based mapping, for deep mapping:


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




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