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>
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With