JSON

PHP code to convert XML to JSON

Converting XML to JSON is very easy in PHP. See the following code.

$jsonfile =  fopen("file.json", "w") or die ("Cannot open JSON file for writing");
$xml = simplexml_load_file("file.xml") or die ("Cannot load xml file");
fwrite($jsonfile, json_encode($xml));
fclose($jsonfile);

line 1: open an empty file to write JSON line 2: load xml file line 3: convert xml to json using json_encode function and then write the json file line 4: close the file