{"id":4258,"date":"2022-05-09T13:13:03","date_gmt":"2022-05-09T12:13:03","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=4258"},"modified":"2026-03-04T19:42:02","modified_gmt":"2026-03-04T19:42:02","slug":"convert-to-xml","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/php\/xml\/convert-to-xml","title":{"rendered":"Convert to XML"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Convert PHP array to XML<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">Conversion function<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/******************************************\n\/\/******************************************\n\/\/********** CONVERT ARRAY TO XML **********\n\/\/******************************************\n\/\/******************************************\n\/\/We use our own function as array_walk_recursive() adds array items the wrong way round (&lt;value&gt;name&lt;\/value&gt;)\nfunction ArrayToXml($Array, &amp;$XmlData)\n{\n  foreach ($Array as $key =&gt; $value)\n  {\n    \/\/If there is nested array\n    if (is_array($value))\n    {\n      if (is_numeric($key))\n        $key = 'item'.$key;     \/\/deal with &lt;0\/&gt;..&lt;n\/&gt; issues\n\n      $subnode = $XmlData-&gt;addChild($key);\n      ArrayToXml($value, $subnode);\n    }\n    else\n    {\n      if (is_numeric($key))\n        $key = 'item'.$key;     \/\/deal with &lt;0\/&gt;..&lt;n\/&gt; issues\n      \n      $XmlData-&gt;addChild(\"$key\",htmlspecialchars(\"$value\"));\n    }\n   }\n}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Using it<\/h5>\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\n\n    $test_array = array (\n      'abc' => 'Field1',\n      'def' => 'Field2'\n    );\n\n    $Xml = new SimpleXMLElement('&lt;root\/>');\n    ArrayToXml($test_array, $Xml);\n    print $Xml->asXML();<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Outputting it as pretty XML with newline formatting etc<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>    $Xml = new SimpleXMLElement('&lt;root\/>');\n    ArrayToXml($test_array, $Xml);\n\n    $Dom = new DOMDocument('1.0', 'UTF-8');\n    $Dom->preserveWhiteSpace = False;\n    $Dom->formatOutput = True;\n    $Dom->loadXML($xml->asXML());\n    $PrettyXml = $Dom->saveXML();\n\n    echo $PrettyXml;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Convert PHP array to XML Conversion function Using it Outputting it as pretty XML with newline formatting etc<\/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-4258","post","type-post","status-publish","format-standard","hentry","category-xml"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/4258","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=4258"}],"version-history":[{"count":6,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/4258\/revisions"}],"predecessor-version":[{"id":5304,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/4258\/revisions\/5304"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=4258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=4258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=4258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}