tags:

views:

1039

answers:

5

I have to write a process (in Java) which periodically hits a URL, reads the returned XML document, and persists that data into the DB. This data is further used by my application, so I have modeled them as Hibernate-mapped POJOs.

I can parse the XML and then create appropriate POJOs, but I was looking for a simpler declarative approach. What libraries are available which can take a input configuration and create the POJOs from the XML document?

+1  A: 

You can use XStream to deserialize the XML and map it directly to the Hibernate-mapped POJOs.

Cheers.

bruno conde
Upvoted, XStream has always felt a little easier to work with than JAXB. http://xstream.codehaus.org/
Freiheit
+2  A: 

Another alternative could be JiBX

Also, although you said you don't want to parse the XML, XPath can be a very concise way of extracting the content you are interested in?

toolkit
JiBX looks to be the most versatile - start from code, start from schema, write a binding file by hand.
Karsten Silz
A: 

Using Hibernate you can directly map XML to table. This is experimental feature. Check here http://www.hibernate.org/hib_docs/v3/reference/en-US/html/xml.html

Adi
+1  A: 

JAXB can automatically create classes based on an XML Schema (assuming you have one for the XML source). At runtime, it can then convert the XML document into POJOs representing the XML. It is declarative in that you can tweak the Schema-to-class mapping, a little.

If I understand your task correctly, this is pretty much the use-case JAXB was designed for (though it can do other things too). It's part of Java 1.6 (maybe 1.5 too?), in packages: javax.xml.bind.*

A: 

EclipseLink JAXB (MOXy) has extensions for mapping JPA entities to XML (JPA entities have things like embedded ID classes, lazy loading, and compound key relationships that need special handling), I'm not aware of any other OXM solution that does this.

For more information see:

Blaise Doughan