【问题标题】:How do I Convert jSON to XML如何将 JSON 转换为 XML
【发布时间】:2015-01-13 20:38:21
【问题描述】:

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

$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>

【问题讨论】:

    标签: php xml json simplexml


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      我在使用 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】:

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

            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. ">";
                }
            }
        

        希望对你有帮助

        【讨论】:

          【解决方案4】:

          不要给你的函数提供一个对象,而是尝试提供一个数组:

          $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

          【讨论】:

          • 有没有办法可以将它们格式化为树或节点?我试过header('Content-type: text/xml');,但看起来还是差不多。
          • @Kreightive 将它们格式化为树是什么意思?喜欢用标签正确格式化?我不认为你可以通过简单的 xml 来美化它
          • 是的,正是@Ghost。有什么办法可以吗?
          • @Kreightive 我不认为你可以在 simplexml 中做到这一点,但你可以在 domdocument 中做到这一点,我应该削减它。 stackoverflow.com/questions/1191167/…
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-09
          • 2017-11-24
          • 2014-11-11
          • 1970-01-01
          相关资源
          最近更新 更多