Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the different approaches for transforming XML with Java?

I´m researching how to transform XML from one format to another in a Java project.

What alternatives are there and what are their pros and cons?

Alternatives I´ve found so far:

  1. XSLT/XQuery
  2. XML-binding both formats to Java and do the mapping in Java
  3. Groovy
like image 475
Odinodin Avatar asked Sep 15 '25 10:09

Odinodin


2 Answers

I vote for XSLT. It has the strong advantage that the transformation rules are separated from your java code. So later on, you can easily react to schema changes without touching your code.

And you can test/debug the transformation anywhere - no need to run the application.

like image 133
Andreas Dolk Avatar answered Sep 18 '25 10:09

Andreas Dolk


If you're transforming XML to XML, then you'll only make extra work for yourself if you go via a non-XML representation (such as Java objects). Using a high-level declarative language is the way to go, and that means XSLT or XQuery.

In choosing between XSLT and XQuery, my usual advice is that XSLT is optimized for transformation and XQuery is optimized for query. By "transformation" I mean tasks in which most of the input appears in the output, just in a different form. By "query" I mean extracting nuggets of information from a sea of data.

Of course, all these technologies have a learning curve, and if you have a small one-off job to do, then that can be a valid factor in choosing your tools. But the best advice for a project doing XML and Java is to do as little Java as possible. Thinking about it as "a Java project" is probably a bad starting position.

like image 29
Michael Kay Avatar answered Sep 18 '25 08:09

Michael Kay