Home >Backend Development >PHP Problem >How to convert JSON to XML in PHP?
How does PHP convert JSON to XML?
First use the "file_get_content()" function to read the JSON data;
$json = file_get_contents('./data.js');
Then use the function "json_decode()" to decode the data;
$data = json_decode($json, true);
Then loop the data and splice it into an XML string; finally write it to the file.
$string=""; foreach($data as $k=>$v){ $string .="<".$k.">"; //取得标签数据 $string .=$v; $string .="</".$k.">"; }
Encapsulation example
<?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 . $indent . '<' . $key; if ($value === null) { echo ' />'; } else { echo '>'; if (is_array($value)) { if ($value) { $temporary = $this->getArrayName($key); foreach ($value as $entry) { $this->recurse(array($temporary => $entry), $level + 1); } echo PHP_EOL . $indent; } } else if (is_object($value)) { if ($value) { $this->recurse($value, $level + 1); echo PHP_EOL . $indent; } } else { if (is_bool($value)) { $value = $value ? 'true' : 'false'; } echo $this->escape($value); } echo '</' . $key . '>'; } } } private function escape($value) { // TODO: return $value; } private function getArrayName($parentName) { // TODO: special namding for tag names within arrays return $parentName; }}$exporter = new Exporter();$exporter->export($data);
Recommended tutorial: "PHP"
The above is the detailed content of How to convert JSON to XML in PHP?. For more information, please follow other related articles on the PHP Chinese website!