In order to convert XML to JSON in PHP, we have a function called json_encode function, and this is an inbuilt function in PHP and the procedure to convert XML to JSON is first getting the contents of the XML file by making use of the function _file_get_contents()_to which the URL of the XML file is passed as a parameter, and then the returns, tabs and the newlines are removed, and then the double quotes are replaced by the single quotes, and then the trailing and leading spaces are trimmed to make sure the XML is parsed properly by a simple XML function, and then the final conversion happens using json_encode function.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax to declare Zlib module in PHP:
json_encode(URL_to_the_XML_file)
Where URL_to_the_XML_file is the URL of the XML file, which is to be converted to JSON.
Given below are the examples of PHP XML to JSON:
PHP program to illustrate the conversion of XML to JSON where we provide the URL to the XML file as a parameter to the json_encode function to convert the contents of the XML file to JSON.
Code:
<html> <body> <?php class XmlToJson { public function Parse ("C://Users/admin/Desktop/check.xml") { # Getting the contents of the XML file by making use of the function file_get_contents() to which the URL of the XML file is passed as a paramter $filepath= file_get_contents("C://Users/admin/Desktop/check.xml"); # Removing the tabs, returns and the newlines $filechange = str_replace(array("\n", "\r", "\t"), '', $filepath); # The trailing and leading spaces are trimmed to make sure the XML is parsed properly by a simple XML function. $filetrim = trim(str_replace('"', "'", $filechange)); # The simplexml_load_string() function is called to load the contents of the XML file. $resultxml = simplexml_load_string($filetrim); # The final conversion of XML to JSON is done by calling the json_encode() function. $resultjson = json_encode($resultxml); return $resultjson; } } ?> </body> </html>
Output:
In the above program, we get the XML file contents by making use of the function file_get_contents(), to which the URL of the XML file is passed as a parameter. Then the the tabs, returns and the newlines are removed. Then the double quotes are replaced by the single quotes. Then the trailing and leading spaces are trimmed to make sure the XML is parsed properly by a simple XML function. Then the simplexml_load_string() function is called to load the contents of the XML file. Then the final conversion of XML to JSON is done by calling the json_encode() function.
PHP program to illustrate the conversion of XML to JSON where we provide the URL to the XML file as a parameter to the json_encode function to convert the contents of the XML file to JSON.
Code:
<html> <body> <?php class XmlToJson { public function Parse ("C://Users/admin/Desktop/test.xml") { # Getting the contents of the XML file by making use of the function file_get_contents() to which the URL of the XML file is passed as a paramter $filepath= file_get_contents("C://Users/admin/Desktop/test.xml"); # Removing the tabs, returns and the newlines $filechange = str_replace(array("\n", "\r", "\t"), '', $filepath); # The trailing and leading spaces are trimmed to make sure the XML is parsed properly by a simple XML function. $filetrim = trim(str_replace('"', "'", $filechange)); # The simplexml_load_string() function is called to load the contents of the XML file. $resultxml = simplexml_load_string($filetrim); # The final conversion of XML to JSON is done by calling the json_encode() function. $resultjson = json_encode($resultxml); return $resultjson; } } ?> </body> </html>
Output:
In the above program, we get the XML file contents by making use of the function file_get_contents(), to which the URL of the XML file is passed as a parameter. Then the the tabs, returns and the newlines are removed. Then the double quotes are replaced by the single quotes. Then the trailing and leading spaces are trimmed to make sure the XML is parsed properly by a simple XML function. Then the simplexml_load_string() function is called to load the contents of the XML file. Then the final conversion of XML to JSON is done by calling the json_encode() function.
PHP program to illustrate the conversion of XML to JSON where we provide the URL to the XML file as a parameter to the json_encode function to convert the contents of the XML file to JSON.
Code:
<html> <body> <?php class XmlToJson { public function Parse ("C://Users/admin/Desktop/file.xml") { # Getting the contents of the XML file by making use of the function file_get_contents() to which the URL of the XML file is passed as a paramter $filepath= file_get_contents("C://Users/admin/Desktop/file.xml"); # Removing the tabs, returns and the newlines $filechange = str_replace(array("\n", "\r", "\t"), '', $filepath); # The trailing and leading spaces are trimmed to make sure the XML is parsed properly by a simple XML function. $filetrim = trim(str_replace('"', "'", $filechange)); # The simplexml_load_string() function is called to load the contents of the XML file. $resultxml = simplexml_load_string($filetrim); # The final conversion of XML to JSON is done by calling the json_encode() function. $resultjson = json_encode($resultxml); return $resultjson; } } ?> </body> </html>
Output:
In the above program, we get the XML file contents by making use of the function file_get_contents(), to which the URL of the XML file is passed as a parameter. Then the the tabs, returns and the newlines are removed. Then the double quotes are replaced by the single quotes. Then the trailing and leading spaces are trimmed to make sure the XML is parsed properly by a simple XML function. Then the simplexml_load_string() function is called to load the contents of the XML file. Then the final conversion of XML to JSON is done by calling the json_encode() function.
The above is the detailed content of PHP XML to JSON. For more information, please follow other related articles on the PHP Chinese website!