Here is my variant of JSON to XML conversion. I get an array from JSON using json_decode() function:

$array = json_decode ($someJsonString, true);

Then I convert the array to XML with my arrayToXml() function:

$xml = new SimpleXMLElement('<root/>');
$this->arrayToXml($array, $xml);

Here is my arrayToXml() function:

/**
 * Convert an array to XML
 * @param array $array
 * @param SimpleXMLElement $xml
 */
function arrayToXml($array, &$xml){
    foreach ($array as value) {
        if(is_int($key)){
            $key = "e";
        }
        if(is_array($value)){
            $label = $xml->addChild(this->arrayToXml($value, $label);
        }
        else {
            $xml->addChild(value);
        }
    }
}
Answer from Michael Yurin on Stack Overflow
🌐
GitHub
gist.github.com › ontiuk › 59d029a8ac4bd3fd4102a357e7ce6e6c
Convert jSON object to XML with PHP · GitHub
Convert jSON object to XML with PHP. GitHub Gist: instantly share code, notes, and snippets.
🌐
Aspose
products.aspose.com › aspose.cells › php via java › conversion › json to xml
PHP JSON to XML - JSON to XML Converter | products.aspose.com
November 13, 2025 - Aspose Excel. This comprehensive solution provides PHP developers with a fully integrated approach to convert JSON to XML format, enabling seamless saving of JSON data into XML format using the Aspose.Cells library, all through efficient and customizable PHP code.
Top answer
1 of 4
16

Instead of feeding your function an object, try to feed an array instead:

$jSON = json_decode($raw_data, true);
                            //  ^ add second parameter flag `true`

Example:

function array2xml($array, $xml = false){

    if($xml === false){
        $xml = new SimpleXMLElement('<result/>');
    }

    foreach($array as value){
        if(is_array($value)){
            array2xml($value, $xml->addChild($key));
        } else {
            $xml->addChild(value);
        }
    }

    return $xml->asXML();
}

$raw_data = file_get_contents('http://pastebin.com/raw.php?i=pN3QwSHU');
$jSON = json_decode($raw_data, true);

$xml = array2xml($jSON, false);

echo '<pre>';
print_r($xml);

Sample Output

2 of 4
3

I had problems with numeric tags using Ghost solution. I had to changed it a bit to remove the numeric tags (just adding a n before):

function array2xml($array, $xml = false){

   if($xml === false){
       $xml = new SimpleXMLElement('<result/>');
   }

   foreach($array as value){
       if(is_array($value)){
           array2xml($value, $xml->addChild(is_numeric((string) key):$key));
       } else {
           $xml->addChild(is_numeric((string) key):value);
       }
   }

   return $xml->asXML();
}

Anyways it does not use attributes, and it does not make arrays with numbers as nodes with same name. I have changed it a bit more to:

function array2xml($array, $parentkey="", $xml = false){

   if($xml === false){
       $xml = new SimpleXMLElement('<result/>');
   }

   foreach($array as value){
       if(is_array($value)){
           array2xml($value, is_numeric((string) key):xml->addChild(is_numeric((string) parentkey:$key));
       } else {
           $xml->addAttribute(is_numeric((string) key):value);
       }
   }

   return $xml->asXML();
}

Changing the call to

$xml = array2xml($jSON, "", false);

With this you get a more natural xml output using attributes and having nodes with same name.

🌐
PHPHelp
phphelp.com › general php help
Convert JSON to XML using PHP - General PHP Help - PHPHelp
October 1, 2013 - My Task is to convert the JSON data in to XML. Right now, My array_to_xml function converts the data from the array into <0>Maserati</0><1>BMW</1><2>Mercedes/2><3>Ford<4>Chrysler</4><5>Acura</5><6>Honda</6><0>1</0><1>2</…
🌐
Web Reference
webreference.com › php › basics › json-xml
PHP JSON and XML Processing | Data Interchange Guide | Web Reference
Master PHP JSON and XML handling including encoding, decoding, validation, transformation, and secure data processing with practical examples.
Find elsewhere
🌐
ASRock
tqdev.com › converting json to xml in javascript and php
Converting JSON to XML in JavaScript and PHP | TQdev
February 22, 2017 - In order to support databases that are better at XML than JSON (Yes, I am talking about you SQL Server) I created some code that allows you to convert (lossless) from JSON to XML (and back). Example JSON data: { "depth": false, "model": "TRX-120", "width": 100, "test": [ { "me": null }, 2.5 ], "height": null } XML data: false TRX-120 100 2.5 The functions ’json2xml’ and ‘xml2json’ convert from JSON to XML and back.
🌐
Laracasts
laracasts.com › discuss › channels › tips › converting-json-to-xml
converting json to xml
October 28, 2020 - We cannot provide a description for this page right now
🌐
ReqBin
reqbin.com › json-to-xml
Online JSON to XML Converter
August 31, 2023 - The JSON to XML converter allows you to convert JSON objects into XML data strings. The JSON converter is fully compatible with JSON and supports JSON elements, arrays, attributes, texts, comments, and JSON declarations. The JSON to XML converter produces clean and reversible XML strings; you can convert JSON to XML and then convert it back from XML to JSON to its original form.
🌐
Medium
sergheipogor.medium.com › convert-xml-to-json-like-a-pro-in-php-603d0a3351f1
Convert XML to JSON Like a Pro in PHP! | by Serghei Pogor | Medium
March 21, 2024 - In this example, we decode a JSON string into a PHP object using json_decode(). Once we have the JSON data decoded, we can convert it to XML format.
🌐
GitHub
github.com › tamlyn › xml2json
GitHub - tamlyn/xml2json: XML to JSON converter in PHP
XML to JSON converter in PHP. Contribute to tamlyn/xml2json development by creating an account on GitHub.
Starred by 49 users
Forked by 34 users
Languages   PHP 100.0% | PHP 100.0%
🌐
Aspose
products.aspose.cloud › aspose.cells › php › conversion › json to xml conversion
Convert JSON to XML using PHP - Aspose Cloud
February 5, 2023 - This Cloud SDK empowers PHP developers with powerful functionality and ensures high-quality XML output. // For complete examples and data files, please go to https://github.com/aspose-cells-cloud/aspose-cells-cloud-php/ <?php require_once('vendor\autoload.php'); use \Aspose\Cells\Cloud\Api\CellsApi; $instance = new CellsApi(getenv("ProductClientId"),getenv("ProductClientSecret")); $path ='Book1.json'; $format ='xml'; $password = null; $outPath = null; $result = $this->instance->cellsWorkbookPutConvertWorkBook($path ,$format, $password, $outPath); $size = $result->getSize(); $content = $result->fread($size); $file = fopen("destfile.xml", 'w'); fwrite($file,$content); fclose($file);
🌐
PHP
php.net › manual › en › book.simplexml.php
PHP: SimpleXML - Manual
Learn How To Improve This Page ... years ago · Three line xml2array: <?php $xml = simplexml_load_string($xmlstring); $json = json_encode($xml); $array = json_decode($json,TRUE); ?> Ta da!...
🌐
Code Beautify
codebeautify.org › jsontoxml
Best JSON to XML Converter to convert JSON to XML String, File, URL
Best Online JSON to XML Converter, Transformer Online Utility. Load form URL, Download, Save and Share.
🌐
Runoob
runoob.com › w3cnote › php-xml2json.html
PHP XML 与 JSON 相互转换 | 菜鸟教程
以下代码将 JSON 数据格式作为 XML 输出: · <?php $json = stream_get_contents(STDIN); $data = @json_decode($json, false); if (!is_array($data) && !is_object($data)) { echo 'ERROR: Invalid JSON given' . PHP_EOL; exit(1); } class Exporter { private $root = 'document'; private $indentation = ' '; // TODO: private $this->addtypes = false; // type="string|int|float|array|null|bool" public function export($data) { $data = array($this->root => $data); echo '<?xml version="1.0" encoding="UTF-8">'; $this->recurse($data, 0); echo PHP_EOL; } private function recurse($data, $level) { $indent = str_repeat($this->indentation, $level); foreach ($data as $key => $value) { echo PHP_EOL .
🌐
Quora
quora.com › How-can-I-convert-XML-to-JSON-in-PHP
How to convert XML to JSON in PHP - Quora
Answer (1 of 5): By using just these two core features of PHP, it is possible to convert any arbitraryXML data into JSON. First, you need to convert the XML content into a suitable PHPdata type using SimpleXMLElement . Then, you feed the PHP ...
🌐
Edopedia
edopedia.com › blog › php-convert-json-to-xml-file
PHP 8 Convert JSON to XML File [Latest Method]
September 13, 2022 - Skip to content · Published on: September 13, 2022 by Furqan · function array2xml($array, $xml = false){ if($xml === false){ $xml = new SimpleXMLElement('<result/>'); } foreach($array as $key => $value){ if(is_array($value)){ array2xml($value, $xml->addChild($key)); } else { $xml->addChild($key, $value); } } return $xml->asXML(); } $raw_data = file_get_contents('http://pastebin.com/raw.php?i=pN3QwSHU'); $jSON = json_decode($raw_data, true); $xml = array2xml($jSON, false); echo '<pre>'; print_r($xml); Website Alexa Rank Checker Using PHP [Latest Method] Download PDF File From URL Using jQuery AJAX Method ·