Home  >  Article  >  Backend Development  >  How to convert JSON to XML in PHP?

How to convert JSON to XML in PHP?

Guanhui
GuanhuiOriginal
2020-07-23 10:54:322566browse

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 &#39;ERROR: Invalid JSON given&#39; . PHP_EOL;
    exit(1);
}
class Exporter
{
    private $root = &#39;document&#39;;
    private $indentation = &#39;    &#39;;
    // TODO: private $this->addtypes = false; // type="string|int|float|array|null|bool"
    public function export($data)
    {
        $data = array($this->root => $data);
        echo &#39;<?xml version="1.0" encoding="UTF-8">&#39;;
        $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 . &#39;<&#39; . $key;
            if ($value === null) {
                echo &#39; />&#39;;
            } else {
                echo &#39;>&#39;;
                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 ? &#39;true&#39; : &#39;false&#39;;
                    }
                    echo $this->escape($value);
                }
                echo &#39;</&#39; . $key . &#39;>&#39;;
            }
        }
    }
    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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn