How do I Convert jSON to XML

30,792

Solution 1

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

Solution 2

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

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.

Solution 3

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

Share:
30,792
Intact Dev
Author by

Intact Dev

Updated on November 24, 2021

Comments

  • Intact Dev
    Intact Dev over 2 years

    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.

    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>