Like all OpenXML files, open office Excel (XLSX) files are compressed zip archives consisting of several XML document definitions, plain text metadata, embedded objects, and a few other types of content. This zip archive file structure makes a lot of sense for storing complex spreadsheet information and representing that content in the Excel application, but it makes less sense for sharing and loading spreadsheet data elsewhere on the web.
By converting XLSX directly to XML, we can remove our Excel files’ zip archive structure while retaining many of our cell formatting specifications. XML is, after all, a feature-rich markup language first, and a capable data interchange format second - which stands in stark contrast, for instance, to an object data storage format like JSON, or to a tabular text data format like CSV.
In vanilla XML format, our spreadsheet content will be far easier to integrate with web services, and it’ll be easier to process & manipulate with a wider range of technologies, expanding where we can share and what we can do with our data considerably.
Tutorial: Convert XLSX to XML in Java
In this tutorial, we’ll learn how to make XLSX to XML conversions with a free API. We’ll use Java code examples to structure our API call, and we’ll walk step by step through the specific purposes those examples serve.
Step 1: Install the SDK
To kick off our tutorial, we’ll first install the client SDK using Maven.
Let’s go ahead and add the following reference to the repository in our Maven POM file:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Next, let’s add the following reference to the dependency:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
Step 2: Add the Imports
We’ll now add the Convert API imports to the top of our file. Let’s copy those from the below snippet:
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.ConvertDataApi;
Step 3: Configuration
We’ll now use the following snippet to configure API authorization.
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
We’ll need a free Cloudmersive API key to authorize our request, and we can get one of those by creating a free account on the Cloudmersive website. Free API keys allow a limit of 800 API calls per month (with zero commitments), and once we have one, we can replace the “YOUR API KEY”
placeholder text with our own API key string.
Step 4: Call the Function
In our final step, we’ll create an instance of the XLSX to XML API and call the conversion function. This operation will return our XML file bytes in a string, which we can write to a new XML document:
ConvertDataApi apiInstance = new ConvertDataApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
try {
byte[] result = apiInstance.convertDataXlsxToXml(inputFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertDataApi#convertDataXlsxToXml");
e.printStackTrace();
}
All done - no more code required!
Making XLSX to XML conversions with an API will ensure we get high quality, consistent results, and it’ll help our applications offload file processing memory to an external server.
Look forward to more API tutorials? Don’t forget to subscribe for new articles in your inbox each week!