How to Easily Convert Data to XML in Nodejs

Cloudmersive
3 min readMay 10, 2024

--

We can make common data format conversions to XML easy by calling a few low-code APIs in Nodejs.

All we need to do is copy and paste from the complementary, ready-to-run Nodejs code examples provided below to structure our API calls, and we’ll be able to convert CSV, XLSX (Excel), and JSON (objects or strings) to XML with zero hassle.

The main benefits to using APIs for our conversions rather than built-in programming functions is reliability and consistency. We’ll abstract the conversion process away from our environment, and we’ll avoid having to write and subsequently update new code in the process.

To begin structuring our API calls, let’s first install the client SDK. We can install using NPM install:

npm install cloudmersive-convert-api-client --save

Or we can add the below snippet to our package.json:

  "dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}

Next, we can call a few different functions to make conversions to XML from other data formats.

First, we can convert our CSV data to XML using the below code. Note that we can use the columnNamesFromFirstRow request parameter to customize how our CSV rows are labeled in the XML file:

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;

// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';



var apiInstance = new CloudmersiveConvertApiClient.ConvertDataApi();

var inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Input file to perform the operation on.

var opts = {
'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.
};

var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.convertDataCsvToXml(inputFile, opts, callback);

Next, we can convert our Excel XLSX data to XML using the below code. It’s worth noting that XLSX is an XML-based format, so this conversion is very straightforward:

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;

// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';



var apiInstance = new CloudmersiveConvertApiClient.ConvertDataApi();

var inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Input file to perform the operation on.


var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.convertDataXlsxToXml(inputFile, callback);

Finally, we can use the remaining two functions to convert JSON objects and JSON strings to XML respectively.

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;

// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';



var apiInstance = new CloudmersiveConvertApiClient.ConvertDataApi();

var jsonObject = null; // Object | Input JSON Object to convert to XML


var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.convertDataJsonToXml(jsonObject, callback);

The main difference is how we input our JSON. In the JSON string conversion below, we can replace “jsonString_example” with a string containing our JSON data:

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;

// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';



var apiInstance = new CloudmersiveConvertApiClient.ConvertDataApi();

var jsonString = "jsonString_example"; // String | Input JSON String to convert to XML


var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.convertDataJsonStringToXml(jsonString, callback);

And that’s all there is to it — now we can easily convert a few common data formats to XML using simple, low-code API solutions.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet