Convertir un fichier JSON dans un format XML en PHP
Salut à tous,
Je cherche a convertir un ficher json en XML par PHP, Pour cela j'ai commencé a décoder le contenu de ma ficher en PHP puis j'ai développé une fonction
qui fait la conversion de PHP en XML. le programme m'affiche un erreur.
voici le code en PHP
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
<?php
function array_to_xml($array, &$xml_data) {
foreach($array as $key => $value) {
if(is_array($value)) {
if(!is_numeric($key)){
$subnode = $xml__data->addChild("$key");
array_to_xml($value, $subnode);
}
else{
array_to_xml($value, $xml_data);
}
}
else {
$xml_data->addChild($key, htmlspecialchars($value));
}
}
}
// Gets JSON file
$file = file_get_contents("C:/Users/DELL/Desktop/f1.json");
$contents = utf8_encode($file);
$array = json_decode($contents);
// creating object of SimpleXMLElement
$xml_data = new SimpleXMLElement('<?xml version="1.0"?><data></data>');
// function call to convert array to xml
array_to_xml($array,$xml_data);
//saving generated xml file;
$result = $xml_data->asXML('C:/Users/DELL/Desktop/name.xml');
?> |
Citation:
Warning: htmlspecialchars() expects parameter 1 to be string