How to Easily Convert Data to XML in PHP

Cloudmersive
3 min readMay 14, 2024

--

While CSV and JSON might be simpler formats to work with, XML has a few important use-cases we’ll sometimes run into.

When we need XML data for our project, we’ll need to convert our data to XML quickly and efficiently — and the best way to handle that is with a free API. Using code examples provided below, we can call free APIs that convert CSV, XLSX (Excel), and JSON data to XML on our behalf.

To call any of these APIs, we’ll need to first install the PHP client via Composer by executing the below command from our command line:

composer require cloudmersive/cloudmersive_document_convert_api_client

After that, we’ll need to quickly turn our attention to API call authorization. We can authorize all of our API calls using a single free Cloudmersive API key, and that will give us a limit of 800 API calls per month with zero commitments.

With our API key ready to go, we can now copy code examples to call our data conversion function.

We can use the below code to convert CSV data to XML (note that we can use the $column_names_from_first_row parameter to customize how column headings are handled in our conversion):

<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');



$apiInstance = new Swagger\Client\Api\ConvertDataApi(


new GuzzleHttp\Client(),
$config
);
$input_file = "/path/to/inputfile"; // \SplFileObject | Input file to perform the operation on.
$column_names_from_first_row = true; // bool | 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 {
$result = $apiInstance->convertDataCsvToXml($input_file, $column_names_from_first_row);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ConvertDataApi->convertDataCsvToXml: ', $e->getMessage(), PHP_EOL;
}
?>

And we can use the below examples to convert XLSX (Excel) to XML:

<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');



$apiInstance = new Swagger\Client\Api\ConvertDataApi(


new GuzzleHttp\Client(),
$config
);
$input_file = "/path/to/inputfile"; // \SplFileObject | Input file to perform the operation on.

try {
$result = $apiInstance->convertDataXlsxToXml($input_file);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ConvertDataApi->convertDataXlsxToXml: ', $e->getMessage(), PHP_EOL;
}
?>

Finally, we can use the remaining two sets of code examples to convert JSON object and JSON strings to XML respectively.

Use the below for JSON objects:

<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');



$apiInstance = new Swagger\Client\Api\ConvertDataApi(


new GuzzleHttp\Client(),
$config
);
$json_object = new \stdClass; // object | Input JSON Object to convert to XML

try {
$result = $apiInstance->convertDataJsonToXml($json_object);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ConvertDataApi->convertDataJsonToXml: ', $e->getMessage(), PHP_EOL;
}
?>

And use the below for JSON strings:

<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');



$apiInstance = new Swagger\Client\Api\ConvertDataApi(


new GuzzleHttp\Client(),
$config
);
$json_string = "json_string_example"; // string | Input JSON String to convert to XML

try {
$result = $apiInstance->convertDataJsonStringToXml($json_string);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ConvertDataApi->convertDataJsonStringToXml: ', $e->getMessage(), PHP_EOL;
}
?>

No more code required — now we can handle a few common data conversions to XML whenever the need arises.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet