How can I convert jSON to XML?

Before I get started, I know there are a few questions like this, but trust me, I've read all, if not most of them. I've tried a bunch of solutions, but none of them seem to work. As a result, I get an empty "tree". Here is the code 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 I am using.

http://pastebin.com/pN3QwSHU

I'm not sure why it doesn't work. Here is the result when I use this function.

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

      

+3


source to share


4 answers


Instead of feeding your function an object, try feeding an array:

$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

+9


source


It's really not that hard to do, I had a similar problem and I just solved it by doing the following: Thought I'd share it just in case someone tries to do this. This is very easy to do.

    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

+1


source


I had problems with numeric tags using Ghost's solution. I had to modify it a bit to remove the numeric tags (just adding n first):

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

      

It doesn't use attributes anyway, and it doesn't create arrays of numbers as nodes with the same name. I changed it a little more:

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

      

Change call to

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

      

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

+1


source


Built-in functions can do this.

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

      

0


source







All Articles