7

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>
IGP
  • 14,160
  • 4
  • 26
  • 43
Intact Dev
  • 486
  • 3
  • 7
  • 16

4 Answers4

16

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

Kevin
  • 41,694
  • 12
  • 53
  • 70
  • 1
    Is there a way that I can format them as trees or nodes? I tried `header('Content-type: text/xml');` but it still looks relatively the same. – Intact Dev Nov 17 '14 at 01:58
  • @Kreightive what do you mean format them as trees? like properly formatted with tabs? i don't think you can prettify it thru simple xml – Kevin Nov 17 '14 at 01:59
  • Yes, exactly @Ghost. Is there a way that I can? – Intact Dev Nov 17 '14 at 18:53
  • @Kreightive i don't think you can do that in simplexml, you can do it in domdocument though, i this should cut it. http://stackoverflow.com/questions/1191167/format-output-of-simplexml-asxml – Kevin Nov 17 '14 at 23:53
3

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.

Hamboy75
  • 938
  • 1
  • 11
  • 22
2

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

1

The built in functions can do this.

$xml = new SimpleXMLElement('<root/>');
$arr = json_decode($your_array, true);
array_walk_recursive($arr, array ($xml,'addChild'));
jmattheis
  • 10,494
  • 11
  • 46
  • 58
zak
  • 776
  • 10
  • 10