如何将jSON转换为XML

时间:2014-11-17 01:24:13

标签: php xml json simplexml

在我开始之前,我知道有很多类似的问题,但相信我,我读了所有,如果不是大多数。我尝试了一堆解决方案,但似乎都没有。我得到一个空白的“树”作为我的结果。这是我正在使用的代码。

$jSON = json_decode('array here');

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();
}

这是我正在使用的jSON数组。

http://pastebin.com/pN3QwSHU

我不确定为什么它不起作用。这是我使用该功能时的结果。

<result>
<generated_in>155ms</generated_in>
</result>

4 个答案:

答案 0 :(得分:11)

不是向对象提供对象,而是尝试提供数组:

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

示例:

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);

Sample Output

答案 1 :(得分:1)

这真的不是很难做到,我遇到了类似的问题并且通过执行以下操作解决了这个问题,我想我会分享它以防万一其他人试图这样做。这很简单。

    protected function display($result) {
        if (empty($this->output_type) || strtolower($this->output_type) == "json") {
            header('Content-type: application/json');
            echo json_encode($result);
        } else if (strtolower($this->output_type) == "xml") {
            header('Content-type: application/xml');
            echo '<?xml version="1.0"?>';
            echo "<result>";
            $xml_array = json_decode(json_encode($result), true);
            $this->xmlWalker($xml_array, "start");
            echo "</result>";
        } else { //catch all why not
            header('Content-type: application/json');
            echo json_encode($result);
        }
    }

    protected function xmlWalker($xml_array, $parent) {
         foreach($xml_array as $tag => $value) {
            if ((int)$tag === $tag) {
                $tag = mb_substr($parent, 0, -1);
            }
            echo "<" .$tag. ">";
            if (is_array($value)) {
                $this->xmlWalker($value, $tag);
            } else {
                echo $value;
            }
            echo "</" .$tag. ">";
        }
    }

我希望这会有所帮助

答案 2 :(得分:1)

我使用Ghost解决方案时遇到数字标签问题。我不得不改变它以删除数字标签(之前只添加一个n):

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(is_numeric((string) $key)?("n".$key):$key));
       } else {
           $xml->addChild(is_numeric((string) $key)?("n".$key):$key, $value);
       }
   }

   return $xml->asXML();
}

无论如何,它不使用属性,并且它不会将带有数字的数组作为具有相同名称的节点。我已经改变了一点:

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

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

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

   return $xml->asXML();
}

将通话更改为

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

通过这种方式,您可以使用属性和具有相同名称的节点获得更自然的xml输出。

答案 3 :(得分:0)

内置函数可以做到这一点。

$xml = new SimpleXMLElement('<root/>');
$arr = json_decode($your_array, true);
array_walk_recursive($arr, array ($xml,'addChild'));
相关问题