Here is my variant of JSON to XML conversion. I get an array from JSON using json_decode() function:
$array = json_decode ($someJsonString, true);
Then I convert the array to XML with my arrayToXml() function:
$xml = new SimpleXMLElement('<root/>');
$this->arrayToXml($array, $xml);
Here is my arrayToXml() function:
/**
* Convert an array to XML
* @param array $array
* @param SimpleXMLElement $xml
*/
function arrayToXml($array, &$xml){
foreach ($array as
value) {
if(is_int($key)){
$key = "e";
}
if(is_array($value)){
$label = $xml->addChild(
this->arrayToXml($value, $label);
}
else {
$xml->addChild(
value);
}
}
}
Answer from Michael Yurin on Stack OverflowHere is my variant of JSON to XML conversion. I get an array from JSON using json_decode() function:
$array = json_decode ($someJsonString, true);
Then I convert the array to XML with my arrayToXml() function:
$xml = new SimpleXMLElement('<root/>');
$this->arrayToXml($array, $xml);
Here is my arrayToXml() function:
/**
* Convert an array to XML
* @param array $array
* @param SimpleXMLElement $xml
*/
function arrayToXml($array, &$xml){
foreach ($array as
value) {
if(is_int($key)){
$key = "e";
}
if(is_array($value)){
$label = $xml->addChild(
this->arrayToXml($value, $label);
}
else {
$xml->addChild(
value);
}
}
}
Check it here: How to convert array to SimpleXML
and this documentation should help you too
Regarding Json to Array, you can use json_decode to do the same!
Videos
Json & Array from XML in 3 lines:
$xml = simplexml_load_string($xml_string);
$json = json_encode(
array = json_decode($json,TRUE);
Sorry for answering an old post, but this article outlines an approach that is relatively short, concise and easy to maintain. I tested it myself and works pretty well.
http://lostechies.com/seanbiefeld/2011/10/21/simple-xml-to-json-with-php/
<?php
class XmlToJson {
public function Parse ($url) {
$fileContents= file_get_contents(
fileContents = str_replace(array("\n", "\r", "\t"), '', $fileContents);
$fileContents = trim(str_replace('"', "'", $fileContents));
$simpleXml = simplexml_load_string($fileContents);
$json = json_encode($simpleXml);
return $json;
}
}
?>
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
value){
if(is_array($value)){
array2xml($value, $xml->addChild($key));
} else {
$xml->addChild(
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
value){
if(is_array($value)){
array2xml($value, $xml->addChild(is_numeric((string)
key):$key));
} else {
$xml->addChild(is_numeric((string)
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
value){
if(is_array($value)){
array2xml($value, is_numeric((string)
key):
xml->addChild(is_numeric((string)
parentkey:$key));
} else {
$xml->addAttribute(is_numeric((string)
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.