How to Convert XML (string OR file format) to JSON using Java
Converting XML to JSON is a common need for many developers. With a free Cloudmersive account (register on our website) & the help of our XML to JSON Conversion API, this conversion will become a fluid and easy task to complete on a regular basis (for both XML strings AND files).
The best part: you can easily take advantage of this API by following instructions below to structure your API call. We’ve provided ready-to-run code examples in Java for your convenience, but you can easily find additional code examples in 12 other common programming languages by visiting our API console page.
We’re going to begin by installing the Java SDK with Maven. To do so, first add the below reference to the repository in pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Then add the below reference to the dependency:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
Now you can include the remaining code block to complete the API call:
// 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;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.
try {
Object result = apiInstance.convertDataXmlToJson(inputFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertDataApi#convertDataXmlToJson");
e.printStackTrace();
}