Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a roundtrip from XML Schema using Java with a Database

What is the best way of implementing a roundtrip for receiving XML files and eventually persisting the data into a database using Java. Currently I have:
1. An XML Schema & XML data files send to me
- the XSD is fairly complex and belongs to an external party, so I can not change it
2. Created the Java classes
- JAXB generates over 150 classes, unfortunately the schema can change
- I have used Maven POM to automate the process 3. Unmarshall the XML data files into Java objects (with JAXB annotation)
- the data would be displayed to the user to be manipulated
4. Persist the Java objects into a RDBMS (Oracle / MySQL)
- it seems JDO is the most suitable solution
5. Exporting the data
- The data can be exported again as XML or Excel (for example)

I can not find a suitable way to add JDO metadata or annotations to the Java source code (generated during the JAXB process) to ensure I can persist the Java classes. I am working with the following technologies:
- Java, Maven, JAXB, JDO & JDBC

I think DataNucleus would be most suited as I might have to change datastores (RDBMS / XML / Excel) often between environments with different export destinations. The only other two technologies I might need to consider is:
- Spring and XDoclet

Advice or a pointer to a tutorial would be appreciated

like image 492
wacko Avatar asked May 23 '26 12:05

wacko


1 Answers

What is the best way of implementing a roundtrip for receiving XML files and eventually persisting the data into a database using Java.

For this I would use a combination of JAXB and JAXB. Note: I lead EclipseLink JAXB (MOXy), EclipseLink also provides an excellent JPA implementation.

Currently I have:

1 - An XML Schema & XML data files send to me

  • the XSD is fairly complex and belongs to an external party, so I can not change it

Option #1 - Start from XML Schema

  • If you want to start from XML schema you can use JAXB's external binding file to customize the class generation as needed without modifying third party XML Schema.

Option #2 - Start from Java Classes

  • You can also start with your JPA entities and map them to your XML schema.
  • MOXy offers an XPath based mapping extension that can make this mapping easier: http://bdoughan.blogspot.com/2010/09/xpath-based-mapping-geocode-example.html

2 - Created the Java classes

  • JAXB generates over 150 classes, unfortunately the schema can change
  • I have used Maven POM to automate the process

If the XML schema changes you can always regenerate your model, or annotate your model as necessary to handle the modifications to the XML schema.

You may find the HyperJAXB project useful, as I believe it will generate JPA annotations right onto a JAXB model.

3 - Unmarshall the XML data files into Java objects (with JAXB annotation)

  • the data would be displayed to the user to be manipulated

This is just normal JAXB usage. For an example see:

  • http://bdoughan.blogspot.com/2010/09/processing-atom-feeds-with-jaxb.html

4 - Persist the Java objects into a RDBMS (Oracle / MySQL)

  • it seems JDO is the most suitable solution

JPA is another alternative.

5 - Exporting the data

  • The data can be exported again as XML or Excel (for example)

Again this is normal JAXB usage to get the XML output.

like image 167
bdoughan Avatar answered May 26 '26 02:05

bdoughan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!