Tools, FAQ, Tutorials:
Convert JSON to XML with PHP
How to convert a JSON text string to an XML document with PHP language?
✍: FYIcenter.com
Currently, there is no built-in function or any standard extension that you can use to convert a JSON text string to an XML document.
But you can use the following PHP example, json_to_xml_converter.php, to convert a JSON text string to an XML document:
<?php # json_to_xml_converter.php # Copyright (c) FYIcenter.com $file = fopen($argv[1], "r") or die("Unable to open json file!"); $json = fread($file,filesize($argv[1])); fclose($file); $val = json_decode($json); if (json_last_error() == JSON_ERROR_NONE) { $dom = new DOMDocument('1.0'); $dom->formatOutput = true; $dom->appendChild(buildXML($dom, "root",$val)); print($dom->saveXML()); } else { print("\njson_decode() error: ".json_last_error_msg()."\n"); } function buildXML($dom, $tag, $val) { $xml = null; if (is_scalar($val)) { $xml = $dom->createElement($tag, $val); } else if (is_array($val)) { $xml = $dom->createElement($tag); foreach ($val as $element) { $xml->appendChild(buildXML($dom,"element",$element)); } } else if (is_object($val)) { $xml = $dom->createElement($tag); foreach ((array)$val as $key=>$property) { $key = keyToTagASCII($key); try { new DOMElement($key); $xml->appendChild(buildXML($dom,$key,$property)); } catch(DOMException $e) { print("Invalid key name: (".$key.") - ".$e->getMessage()."\n"); } } } else { $xml = new DOMElement($tag); } return $xml; } function keyToTagASCII($key) { if (preg_match("/^xml/i", $key)) { $key = "_".$key; } if (preg_match("/^[0-9\.-]/", $key)) { $key = "_".$key; } $key = preg_replace("/[^0-9a-zA-Z\.\-_]/", "_", $key); return $key; } ?>
Note that this PHP example is limited to JSON objects with property names in ASCII characters only. This simplifies the coding effort to convert JSON property names to XML tag names.
More PHP coding is needed to support other non-ASCII characters that are allowed in XML tag names.
To try the above PHP example, you can enter the following JSON file, json_object.json:
{ "a": null, "x+y": "X plus Y", "x-y": "X minus Y", "007": "Double O Seven", "fyi.pi": 0.314159e01, "fyi:pi": 0.314159e01, "fyi_pi": "0.314159e01", "fyi>pi": "31.4159e-1", "error msg": "Something wrong!" }
Then run the above PHP example through the PHP engine with the JSON file provided in the command line:
>\fyicenter\php\php.exe json_to_xml_converter.php json_object.json <?xml version="1.0"?> <root> <a/> <x_y>X plus Y</x_y> <x-y>X minus Y</x-y> <_007>Double O Seven</_007> <fyi.pi>3.14159</fyi.pi> <fyi_pi>3.14159</fyi_pi> <fyi_pi>0.314159e01</fyi_pi> <fyi_pi>31.4159e-1</fyi_pi> <error_msg>Something wrong!</error_msg> </root>
⇒ JSON to XML Converter at fyicenter.com
2023-07-11, 9786🔥, 0💬
Popular Posts:
How To Use an Array as a Queue in PHP? A queue is a simple data structure that manages data elements...
Where to find tutorials on RSS specifications? I want to learn it to describe my API services. Here ...
Where to find tutorials on Visual Studio? I want to know How to learn Visual Studio. Here is a large...
FYIcenter.com Online Tools: FYIcenter JSON Validator and Formatter FYIcenter JSON to XML Converter F...
How To Set session.gc_divisor Properly in PHP? As you know that session.gc_divisor is the frequency ...