tags:

views:

187

answers:

8

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.

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?

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.

+3  A: 

i use this: Simple

pstanton
This seems to be entirely annotation based. I want to be able to accomplish my goal strictly through a config file and potentially a set of translator objects to properly format some data. Additionally, I don't see any way of specifying a single input property to multiple output fields.
Jherico
upvoted, since this did answer the original question.
Paul Wagland
Not so much. I thought I made it pretty apparent I *wanted* a configuration file.
Jherico
+1  A: 

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

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.

Paul Wagland
Castor looks to be closest to what I'm looking for of everything I've seen. However, its class-centric configuration makes me wonder if it allows for multiple mappings of the same input field to multiple outputs. I also don't see any examples of mapping deep fields. I'll investigate further though.
Jherico
A: 

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.

Peter Knego
A: 

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

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.

Fortyrunner
The downside is that if you mess up the templates you can end up generating invalid/incorrect XML in some circumstances.
Stephen C
Totally agree. This isn't difficult to fix though. You can add a separate validation phase if you wish.
Fortyrunner
+1  A: 

Xstream is good

Sri Kumar
A: 

The solution you want is 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:

Blaise Doughan
A: 

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.

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

Guillaume
+1  A: 

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

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

Jon