How to Convert CSV Files to XML Files using Java

When you’re looking for a flexible, nested data format rather than the flat, tabular layout that CSV files provide, XML is the way to go. Our CSV to XML conversion API makes it easy to perform this common operation automatically — all you need to do is follow instructions below to structure your API call in Java (and register a free account on our website to get your Cloudmersive API key).

Let’s begin by installing the Java SDK with Maven. Copy in the below references first; these belong in the pom.xml repository and dependency respectively:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>

After that, let’s add in our import classes:

// 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;

Now you can complete the call by including the remaining code block:

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");
ConvertDataApi apiInstance = new ConvertDataApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
Boolean columnNamesFromFirstRow = true; // Boolean | Optional; If true, the first row will be used as the labels for the columns; if false, columns will be named Column0, Column1, etc. Default is true. Set to false if you are not using column headings, or have an irregular column structure.
try {
byte[] result = apiInstance.convertDataCsvToXml(inputFile, columnNamesFromFirstRow);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertDataApi#convertDataCsvToXml");
e.printStackTrace();
}

All that’s left now is to input your API key where denoted in the documentation, and you’re all set. You can also optionally include a Boolean to specify if the first row of your CSV table should be used as the label for the columns.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Cloudmersive

Cloudmersive

437 Followers

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.