Home  >  Article  >  Backend Development  >  PHP XML to JSON

PHP XML to JSON

WBOY
WBOYOriginal
2024-08-29 13:00:46573browse

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 Tests

Start 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.

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:

PHP XML to JSON

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:

PHP XML to JSON

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:

PHP XML to JSON

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP Object SerializationNext article:PHP Object Serialization