Updated April 4, 2023
Introduction to PHP XML to JSON
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.
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.
Steps to Convert XML to JSON in PHP
- Getting the XML file contents by using the function file_get_contents() to which the URL of the XML file is passed as a parameter.
- Removing the tabs, returns and the newlines.
- The single quotes replace the double-quotes.
- The trailing and leading spaces are trimmed to make sure the XML is parsed properly by a simple XML function.
- The simplexml_load_string() function is called to load the contents of the XML file.
- The final conversion of XML to JSON is done by calling the json_encode() function.
Examples of PHP XML to JSON
Given below are the examples of PHP XML to JSON:
Example #1
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.
Example #2
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.
Example #3
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.
Recommended Articles
This is a guide to PHP XML to JSON. Here we discuss the introduction, steps to convert XML to JSON in PHP and examples, respectively. You may also have a look at the following articles to learn more –