Convert XML to JSON (Java)

In this post, we present how to convert an XML document into JSON. In most cases, working with JSON documents is much more preferable compared to working with XML documents. To do so, we will make use of the following library.

A full maven example using arxiv’s metadata XML is available on github.

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20140107</version>
</dependency>

Utilizing the above library the convetion is pretty straighforward, i.e.,

int PRETTY_PRINT_INDENT_FACTOR = 4;
File file = new File("arxivMetadata.xml");
// Convert file contents to string
String contents = FileUtils.readFileToString(file);
// Convert XML to JSON
JSONObject xmlJSONObj = XML.toJSONObject(contents);
// Print XML to papers.json
String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);
PrintWriter writer = new PrintWriter("papers.json");
writer.write(jsonPrettyPrintString);
writer.close();

The above code reads the XML file arxivMetadata.xml, converts it to string using commons-io, then converts it to a JSONObject and finally prints the JSON to papers.json.

References

Written on September 20, 2015