{"id":610,"date":"2012-07-16T07:17:18","date_gmt":"2012-07-16T07:17:18","guid":{"rendered":"http:\/\/codeatelier.wordpress.com\/?p=65"},"modified":"2012-07-16T07:17:18","modified_gmt":"2012-07-16T07:17:18","slug":"xml-file-generation-in-php","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/xml-file-generation-in-php\/","title":{"rendered":"XML File Generation In PHP"},"content":{"rendered":"<p><strong>Creating Simple XML File In PHP<\/strong><\/p>\n<pre>\u00a0&lt;?php\u00a0\u00a0 \u00a0\n\u00a0\u00a0 \u00a0header(\"Content-Type: text\/plain\");\n\n\u00a0\u00a0 \u00a0\/\/create the xml document\n\u00a0\u00a0 \u00a0$xmlDoc = new DOMDocument();\n\n\u00a0\u00a0 \u00a0\/\/create the root element\n\u00a0\u00a0 \u00a0$root = $xmlDoc-&gt;appendChild(\n\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $xmlDoc-&gt;createElement(\"Students\"));\n\n\u00a0\u00a0 \u00a0\/\/make the output pretty\n\u00a0\u00a0 \u00a0$xmlDoc-&gt;formatOutput = true;\n\n\u00a0\u00a0 \u00a0echo $xmlDoc-&gt;saveXML();\n?&gt;<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>&lt;?xml version=\"1.0\"?&gt;\n&lt;Students\/&gt;<\/pre>\n<p><strong>Adding Nodes To XML Document<\/strong><\/p>\n<pre>\/\/create a tutorial element\n\u00a0 $tutTag = $root-&gt;appendChild(\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $xmlDoc-&gt;createElement(\"Student\"));\n\n\u00a0 \/\/create the author attribute\n\u00a0 $tutTag-&gt;appendChild(\n\u00a0\u00a0\u00a0 $xmlDoc-&gt;createAttribute(\"id\"))-&gt;appendChild(\n\u00a0\u00a0\u00a0\u00a0\u00a0 $xmlDoc-&gt;createTextNode('105'));\n\n\u00a0 \/\/create the title element\n\u00a0 $tutTag-&gt;appendChild(\n\u00a0\u00a0\u00a0 $xmlDoc-&gt;createElement(\"FirstName\", 'Mugil'));\n\n\u00a0 \/\/create the title element\n\u00a0 $tutTag-&gt;appendChild(\n\u00a0\u00a0\u00a0 $xmlDoc-&gt;createElement(\"LastName\", 'Vannan'));\n\n\u00a0 \/\/create the date element\n\u00a0 $tutTag-&gt;appendChild(\n\u00a0\u00a0\u00a0 $xmlDoc-&gt;createElement(\"Age\", '24'));<\/pre>\n<p><strong>Whole Code<\/strong><\/p>\n<pre> &lt;?php\u00a0\u00a0 \u00a0\n\u00a0\u00a0 \u00a0header(\"Content-Type: text\/plain\");\n\n\u00a0\u00a0 \u00a0\/\/create the xml document\n\u00a0\u00a0 \u00a0$xmlDoc = new DOMDocument();\n\n\u00a0\u00a0 \u00a0\/\/create the root element\n\u00a0\u00a0 \u00a0$root = $xmlDoc-&gt;appendChild(\n\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $xmlDoc-&gt;createElement(\"Students\"));\n\n\u00a0\u00a0 \u00a0\/\/create a tutorial element\n\u00a0 $tutTag = $root-&gt;appendChild(\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $xmlDoc-&gt;createElement(\"Student\"));\n\n\u00a0 \/\/create the author attribute\n\u00a0 $tutTag-&gt;appendChild(\n\u00a0\u00a0\u00a0 $xmlDoc-&gt;createAttribute(\"id\"))-&gt;appendChild(\n\u00a0\u00a0\u00a0\u00a0\u00a0 $xmlDoc-&gt;createTextNode('105'));\n\n\u00a0 \/\/create the title element\n\u00a0 $tutTag-&gt;appendChild(\n\u00a0\u00a0\u00a0 $xmlDoc-&gt;createElement(\"FirstName\", 'Mugil'));\n\n\u00a0 \/\/create the title element\n\u00a0 $tutTag-&gt;appendChild(\n\u00a0\u00a0\u00a0 $xmlDoc-&gt;createElement(\"LastName\", 'Vannan'));\n\n\u00a0 \/\/create the date element\n\u00a0 $tutTag-&gt;appendChild(\n\u00a0\u00a0\u00a0 $xmlDoc-&gt;createElement(\"Age\", '24'));\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\n\n\u00a0\u00a0 \u00a0\/\/make the output pretty\n\u00a0\u00a0 \u00a0$xmlDoc-&gt;formatOutput = true;\n\n\u00a0\u00a0 \u00a0echo $xmlDoc-&gt;saveXML();\n?&gt;<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>&lt;?xml version=\"1.0\"?&gt;\n&lt;Students&gt;\n  &lt;Student id=\"105\"&gt;\n    &lt;FirstName&gt;Mugil&lt;\/FirstName&gt;\n    &lt;LastName&gt;Vannan&lt;\/LastName&gt;\n    &lt;Age&gt;24&lt;\/Age&gt;\n  &lt;\/Student&gt;\n&lt;\/Students&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Creating Simple XML File In PHP \u00a0&lt;?php\u00a0\u00a0 \u00a0 \u00a0\u00a0 \u00a0header(&#8220;Content-Type: text\/plain&#8221;); \u00a0\u00a0 \u00a0\/\/create the xml document \u00a0\u00a0 \u00a0$xmlDoc = new DOMDocument(); \u00a0\u00a0 \u00a0\/\/create the root element \u00a0\u00a0 \u00a0$root = $xmlDoc-&gt;appendChild( \u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $xmlDoc-&gt;createElement(&#8220;Students&#8221;)); \u00a0\u00a0 \u00a0\/\/make the output pretty \u00a0\u00a0 \u00a0$xmlDoc-&gt;formatOutput = true; \u00a0\u00a0 \u00a0echo $xmlDoc-&gt;saveXML(); ?&gt; Output &lt;?xml version=&#8221;1.0&#8243;?&gt; &lt;Students\/&gt; Adding Nodes To XML Document&hellip; <a href=\"https:\/\/codethataint.com\/blog\/xml-file-generation-in-php\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[90],"tags":[],"class_list":["post-610","post","type-post","status-publish","format-standard","hentry","category-php-file-handling"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/610","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=610"}],"version-history":[{"count":0,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/610\/revisions"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}