I am trying to find a library that allows me to place the content of an XML into a JavaBean (something like Digester, Jaxb, JIXB etc) but I need it to be runtime (not at compile time, or by byte code generation) and use a mapping file of some sort.
The idea will be something like Hibernate's HBM mapping files, a way to specify which XML element goes into which Java property. I am currently using Digester but I want something simpler for its rules file.
This should also work on a 1.4 JDK so annotations won't really do (but I will consider such responses just for completeness's sake).
To resume, I need a runtime tool that does XML to Java based on a XML descriptor of some sort, something like this (taken from JIXB) but at runtime (i.e. pass it the XML, the Java class to output object and the mapping descriptor):
Do you know of such a library?
Thank you!
Hibernate actually has some support for XML mapping. See the doc for more infos. I did a PoC some time ago, and found it a bit lacking on the documentation. I didnt find how to use namespaces properly.
We finally used Rome as we only needed to parse / generate Atom feeds with custom namespaces. But I dont think Rome will solve your problem. The mappings are done in Java code, and it is restricted in parsing / generating RSS / Atom ...
Good luck ! And if you end up using Hibernate, I'd love to know how it is working for you !
EclipseLink JAXB (MOXy) (I'm the tech lead) has an XML mapping file. For your example the mapping file would be:
binding.xml
Domain Model
This would map the following classes:
XML
To/from the following XML:
Demo Code
As demonstrated by:
jaxb.properties
To use MOXy as your JAXB implementation you need to include a jaxb.properties file with your model classes with the following entry:
Note
This example uses the simplified bootstrapping available in the upcoming EclipseLink 2.2 release. For an example using EclipseLink 2.1 see:
Try XSLT.
You can take you input XML file and transform it in another XML file that will be the input for Jaxb/XmlBeans/... to populate your bean.
The XSL file will be the "runtime" configuration that will describe the mapping.
Input XML ---(XSLT)---> Bean XML ---(Jaxb)---> Java bean
Apache Commons Digester should probably be a interesting tool to consider. It does what you need, i.e. take a XML and transform it into a Java bean.
Try Castor. It is able to generate java code from DTD (and probably from XSD too). So, you can generate the code at runtime and then compile it.
BTW check JAXB again. I believe that it can do the same.