PHP如何將JSON轉換為XML?
首先使用「file_get_content()」函數將JSON資料讀取出來;
$json = file_get_contents('./data.js');
然後使用函數「json_decode()」將資料解碼;
$data = json_decode($json, true);
接著循環資料拼接成XML字串;最後寫入檔案即可。
$string=""; foreach($data as $k=>$v){ $string .="<".$k.">"; //取得标签数据 $string .=$v; $string .="</".$k.">"; }
封裝範例
<?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);
推薦教學:《PHP》
以上是PHP如何將JSON轉換為XML?的詳細內容。更多資訊請關注PHP中文網其他相關文章!