Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I Convert jSON to XML

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>
like image 258
Intact Dev Avatar asked Nov 17 '14 01:11

Intact Dev


People also ask

Is XML and JSON file same?

JSON is a data interchange format and only provides a data encoding specification. XML is a language to specify custom markup languages, and provides a lot more than data interchange. With its strict semantics, XML defined a standard to assert data integrity of XML documents, of any XML sub-language.

Can XML replace JSON?

Short version: XML is not equivalent to JSON (or similar) format since XML nodes (tags) support attribute notation and namespacing. It turns out to be crucial. So, the best way to answer this question is actually to show how these formats are different, i.e. to complete the comparison.


2 Answers

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

like image 174
Kevin Avatar answered Oct 12 '22 03:10

Kevin


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.

like image 38
hamboy75 Avatar answered Oct 12 '22 04:10

hamboy75