{"id":1164,"date":"2023-11-17T00:00:00","date_gmt":"2023-11-17T05:00:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=1164"},"modified":"2023-11-29T18:43:38","modified_gmt":"2023-11-29T23:43:38","slug":"how-to-convert-xml-to-json-using-php-code","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/how-to-convert-xml-to-json-using-php-code\/","title":{"rendered":"How to convert XML to JSON using PHP code"},"content":{"rendered":"\n<p>This post show how to convert XML to JSON using PHP code.<\/p>\n\n\n\n<p><strong>json_encode() function converts valid XML to JSON<\/strong><\/p>\n\n\n\n<p>Following is example:<\/p>\n\n\n\n<p>In this example, we have an empty data.json file and a data.xml with the following content<\/p>\n\n\n\n<p><strong>data.xml<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;staff>\r\n    &lt;employee>\r\n        &lt;name id=\"1\">Alice&lt;\/name>\r\n        &lt;dob>January 1, 2000&lt;\/dob>\r\n    &lt;\/employee>\r\n    &lt;employee>\r\n        &lt;name id=\"2\">Bob&lt;\/name>\r\n        &lt;dob>December 31, 2000&lt;\/dob>\r\n    &lt;\/employee>\r\n&lt;\/staff><\/pre>\n\n\n\n<p>The following code will read this XML file, generate JSON and output it to data.json file.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?php\r\n    $myJSON =  fopen(\"data.json\", \"w\");\r\n    $myXML = simplexml_load_file(\"data.xml\");\r\n    fwrite($myJSON, json_encode($myXML));\r\n    fclose($myJSON);\r\n?><\/pre>\n\n\n\n<p>Line 1: opens data.json file with \u201cw\u201d option meaning you can write content to it<\/p>\n\n\n\n<p>Line 2: opens data.xml file and loads its data. Note the use of&nbsp;<strong>simple_load_file()<\/strong>&nbsp;function.<\/p>\n\n\n\n<p>Line 3: converts XML to JSON and writes the JSON into data.json file. Note the use of&nbsp;<strong>json_encode()<\/strong>&nbsp;function to convert data.<\/p>\n\n\n\n<p>Line 4: closes data.json file for writing<\/p>\n\n\n\n<p><strong>data.json<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{\r\n    \"staff\": {\r\n        \"employee\": [\r\n            {\r\n                \"name\": {\r\n                    \"-id\": \"1\",\r\n                    \"#text\": \"Alice\"\r\n                },\r\n                \"dob\": \"January 1, 2000\"\r\n            },\r\n            {\r\n                \"name\": {\r\n                    \"-id\": \"2\",\r\n                    \"#text\": \"Bob\"\r\n                },\r\n                \"dob\": \"December 31, 2000\"\r\n            }\r\n        ]\r\n    }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This post show how to convert XML to JSON using PHP code. json_encode() function converts valid XML to JSON Following is example: In this example, we have an empty data.json file and a data.xml with the following content data.xml &lt;staff> &lt;employee> &lt;name id=&#8221;1&#8243;>Alice&lt;\/name> &lt;dob>January 1, 2000&lt;\/dob> &lt;\/employee> &lt;employee> &lt;name id=&#8221;2&#8243;>Bob&lt;\/name> &lt;dob>December 31, 2000&lt;\/dob> &lt;\/employee> &lt;\/staff> [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1207,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[423,424,422],"class_list":["post-1164","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-data-format-conversion-in-php","tag-php-programming-tips","tag-php-xml-to-json-conversion"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1164","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=1164"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1164\/revisions"}],"predecessor-version":[{"id":1165,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1164\/revisions\/1165"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media\/1207"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=1164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=1164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=1164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}