Alexander James King

Calling Clojure from Java to transform XML to JSON

June 23, 2014

Working with XML is painful in Java, there are many options such as DOM, SAX, StAX, JAXB, XSLT but they're all verbose compared to the equivalent code in Clojure where you can make use of Maps and Zippers.

In Clojure my approach is usually as follows:

  1. Parse the XML into a map
  2. Navigate the map using a zipper
  3. Create a new map with the values we're interested in
  4. Convert the new map to JSON

This example follows the above process but calls the Clojure code from Java, ideally I'd wrap this up in a library but to keep things simple I've invoked the dependencies from Java prior to calling the transform library.

Source code on GitHub: https://github.com/alexanderjamesking/clj-from-java-xml-to-json

Input: champions-league.xml

Output: sample-output.json

Transform.java

transform.clj