{"id":4251,"date":"2022-05-09T12:20:21","date_gmt":"2022-05-09T11:20:21","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=4251"},"modified":"2024-02-27T21:04:35","modified_gmt":"2024-02-27T21:04:35","slug":"simplexml","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/php\/xml\/simplexml","title":{"rendered":"simpleXML"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Is simpleXML installed?<\/h4>\n\n\n\n<p>It should be there by default with php, but it seems to be omitted in some installs (e.g. V7 PHP on ubuntu)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    \/\/Make sure simplexml is installed (it should be there by default with php, but it seems to be omitted in some installs.\n    if (!function_exists('simplexml_load_file'))\n      die('Server config issue - simpleXML functions are not available.');    \/\/You can install this on ubuntu using: sudo apt-get install php-xml<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Read values<\/h4>\n\n\n\n<p>For example, this XML file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;RootElement&gt;\n  &lt;MyXmlChildName&gt;1234&lt;\/MyXmlChildName&gt;\n&lt;\/RootElement&gt;<\/code><\/pre>\n\n\n\n<p>Reading a value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  $xml = simplexml_load_file($_SERVER&#91;'DOCUMENT_ROOT'] .  \"\/my_folder\/my_file_name.xml\");\n  if ($xml !== False)   \/\/Was file loaded OK?\n  {\n    if (isset($xml-&gt;MyXmlChildName))        \/\/Check child exisrts\n      echo $xml-&gt;MyXmlChildName . \"&lt;br&gt;\";   \/\/Get its value\n  }<\/code><\/pre>\n\n\n\n<p>Read XML to array example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    \/\/OPEN OUR XML FILE AND GET THE EXCHANGE RATES\r\n    $output = array();\r\n    $xml = simplexml_load_file('exchangeRates.xml');\r\n    if ($xml !== False)   \/\/Was file loaded OK?\r\n    {\r\n        foreach ($xml as $key => $value) {\r\n            \/\/echo $key . \"-\" . $value . \"&lt;br>\";\r\n            $output&#91;$key] = (string)$value;\r\n        }\r\n    }<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Write XML File<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>    $currentTimestamp = date('Y-m-d H:i:s');\n\n\n    \/\/Create XML file\n    $xml = new SimpleXMLElement('&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;&lt;root&gt;&lt;\/root&gt;');\n    $xml-&gt;addChild('ratesLastUpdated', $currentTimestamp);\n    \/\/$xml-&gt;asXml('exchangeRates.xml');    \/\/&lt;&lt;This won't format the XML doc nicely with line breaks\n    \/\/ Save the merged XML to the output file with formatting:\n    $dom = new DOMDocument(\"1.0\");\n    $dom-&gt;preserveWhiteSpace = false;\n    $dom-&gt;formatOutput = true;\n    $dom-&gt;loadXML($xml-&gt;asXML());\n    $dom-&gt;save('exchangeRates.xml');<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Is simpleXML installed? It should be there by default with php, but it seems to be omitted in some installs (e.g. V7 PHP on ubuntu) Read values For example, this XML file: Reading a value: Read XML to array example Write XML File<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[337],"tags":[],"class_list":["post-4251","post","type-post","status-publish","format-standard","hentry","category-xml"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/4251","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=4251"}],"version-history":[{"count":5,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/4251\/revisions"}],"predecessor-version":[{"id":4590,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/4251\/revisions\/4590"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=4251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=4251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=4251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}