如何将jSON转换为XML

时间:2021-09-07 23:44:46

Before I begin, I know there are a bunch of questions similar to this, but trust me, I read all, if not most of them. I tried a bunch of solutions, but none of them seem to work. I'm getting a blank "tree" as my result. Here is the code that I am using.

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

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

Here is the jSON array that I'm using.

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

http://pastebin.com/pN3QwSHU

I'm not sure why it isn't working. Here is the result when I use that function.

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

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

4 个解决方案

#1


10  

Instead of feeding your function an object, try to feed an array instead:

而不是为您的函数提供一个对象,而是尝试提供一个数组:

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

Example:

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

#2


1  

This really isn't too hard to do, I had a similar problem and just solved it by doing the following, thought I would share it just in case anyone else was trying to do it. It is done very simply.

这真的不是很难做到,我有一个类似的问题,只是通过做以下事情解决它,以为我会分享它,以防万一其他人试图这样做。这很简单。

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

I hope this helps

我希望这有帮助

#3


1  

I had problems with numeric tags using Ghost solution. I had to changed it a bit to remove the numeric tags (just adding a n before):

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

Anyways it does not use attributes, and it does not make arrays with numbers as nodes with same name. I have changed it a bit more to:

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

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

Changing the call to

将通话更改为

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

With this you get a more natural xml output using attributes and having nodes with same name.

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

#4


0  

The built in functions can do this.

内置函数可以执行此操作。

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

#1


10  

Instead of feeding your function an object, try to feed an array instead:

而不是为您的函数提供一个对象,而是尝试提供一个数组:

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

Example:

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

#2


1  

This really isn't too hard to do, I had a similar problem and just solved it by doing the following, thought I would share it just in case anyone else was trying to do it. It is done very simply.

这真的不是很难做到,我有一个类似的问题,只是通过做以下事情解决它,以为我会分享它,以防万一其他人试图这样做。这很简单。

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

I hope this helps

我希望这有帮助

#3


1  

I had problems with numeric tags using Ghost solution. I had to changed it a bit to remove the numeric tags (just adding a n before):

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

Anyways it does not use attributes, and it does not make arrays with numbers as nodes with same name. I have changed it a bit more to:

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

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

Changing the call to

将通话更改为

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

With this you get a more natural xml output using attributes and having nodes with same name.

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

#4


0  

The built in functions can do this.

内置函数可以执行此操作。

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