Java: XML into a Database, whats the simplest way? Java: XML into a Database, whats the simplest way? xml xml

Java: XML into a Database, whats the simplest way?


Or you could bypass the step of translating into POJOs and store the XML directly as a CLOB. It'll allow "duck typing" later on, which you might find advantageous.

Mapping to Java POJOs make sense if you need to query for those objects individually later on. If you need the entire stream, all the time, without ever having to query for values in the XML (e.g., XPath), then I'd say that storing XML as a CLOB makes more sense.


Quick answer: JAXB, JPA and Spring

When inserting XML into a database you need to consider what operations you'd like to perform on the data that the XML is representing.

You could, for example, consider the XML to be the input data and then create a schema that holds the data in an easily queryable manner. If that's what you'd like to do then use JAXB as the unmarshaller because you can easily generate suitably annotated pojos/entities from the XSD via the xjc tool. A bit of additional JPA annotating and you'll have a quick solution that maps the XML to a complete schema that allows a variety of mix and match queries and alternative views. Of course, JAXB annotations can be used to generate a wide variety of output formats (XML, JSON, YAML etc) so you're not limited to XML when you want to output this data.

Next, you could consider the XML to be the complete entity that you wish to store. In that case you want to store it either as a CLOB or as XML (in Oracle). Oracle certainly supports XPath based searches so you'd get a good opportunity for querying the resulting dataset.

Finally, if you're thinking that XML is too bloated, and you're in control of any resulting changes to the pojos you could serialize the unmarshalled pojos directly into the database as BLOBs. You'll have a fairly compact schema and database, but you'll suffer when it comes to querying since it's all gonna be binary. And you'll have binary version compatibility issues later on if you have to deserialize a very old dataset based on old pojos.

So, to summarise, JAXB is a very good way to handle the unmarshalling and later marshalling processes. It's quick and simple and (nod to @Blaise Doughan here) very well supported on SO for one thing. JPA is the technology of choice to perform your database operations. Hibernate is one implementer of JPA (with good extensions), and Spring supports it beautifully through the HibernateTemplate. Equally, you can use Spring's JpaTemplate which has, perhaps, a slightly shallower learning curve.


A common approach would be to use JPA (i.e. EclipseLink or Hibernate) to store the objects into the database, and JAXB (i.e. Metro, EclipseLink MOXy (I'm the tech lead), or JaxMe) to convert the objects to XML.

EclipseLink JAXB (MOXy) has a number of extensions for mapping JPA entities to XML: