How to Convert a JSON Object or String to XML using Java
Looking to easily transition your data between common interchange formats? Our Data Conversion API endpoint offers dozens of useful data format conversion options. That includes one of the most common requests out there: converting JSON to XML. To take advantage of our JSON to XML API, refer to the ready-to-run code examples and instructions provided below to structure your API call in Java.
Let’s get started by installing the Java SDK with Maven. We’ll begin by adding a reference to the repository in pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Then we’ll add a reference to the pom.xml dependency:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
After that, add the import classes and call the function. Make sure you include your JSON Object as an input parameter, and include your Cloudmersive API key to authorize access (you can get one by registering a free account on our website, www.cloudmersive.com).
// 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();
Object jsonObject = null; // Object | Input JSON Object to convert to XML
try {
byte[] result = apiInstance.convertDataJsonToXml(jsonObject);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertDataApi#convertDataJsonToXml");
e.printStackTrace();
}
Just like that, you’re all done — no more code required. If you’re looking to convert a JSON String rather than an Object, reuse the imports and use the following code instead:
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();
String jsonString = "jsonString_example"; // String | Input JSON String to convert to XML
try {
Object result = apiInstance.convertDataJsonStringToXml(jsonString);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertDataApi#convertDataJsonStringToXml");
e.printStackTrace();
}