{"id":41,"date":"2008-09-07T16:33:53","date_gmt":"2008-09-07T20:33:53","guid":{"rendered":"http:\/\/www.compdigitec.com\/labs\/?p=41"},"modified":"2008-09-07T16:33:53","modified_gmt":"2008-09-07T20:33:53","slug":"simplexml-solution-for-xml-manipulation-in-php","status":"publish","type":"post","link":"http:\/\/www.compdigitec.com\/labs\/2008\/09\/07\/simplexml-solution-for-xml-manipulation-in-php\/","title":{"rendered":"SimpleXML &#8211; solution for XML manipulation in PHP"},"content":{"rendered":"<p>If you ever do get the need to manipulate (read\/write) XML, you could try using the PHP <a rel=\"nofollow\" href=\"http:\/\/php.net\/dom\">DOM API<\/a>, but it&#8217;s very difficult to use the DOM. An good extension-less way to access and manipulate XML instead is <a href=\"http:\/\/php.net\/simplexml\" rel=\"nofollow\">SimpleXML<\/a>. It&#8217;s very easy to use compared to DOM and features a easy way to access attributes and tags in any XML document.<\/p>\n<p><strong>XML file (sample.xml)<br \/>\n<\/strong>&lt;?xml version=&#8221;1.0&#8243; ?&gt;<br \/>\n&lt;root&gt;&lt;example&gt;This is example number 1&lt;\/example&gt;&lt;example&gt;&lt;tag&gt;This is a tag.&lt;\/tag&gt;&lt;\/example&gt;&lt;\/root&gt;<\/p>\n<p><strong>Reading a XML file<\/strong><code><span style=\"color: #000000;\"><br \/>\n<span style=\"color: #0000bb;\">&lt;?php<br \/>\n<\/span><\/span><\/code>$a\u00a0<span style=\"color: #007700;\">=\u00a0new <\/span><span style=\"color: #0000bb;\">SimpleXMLElement<\/span><span style=\"color: #007700;\">(<\/span><span style=\"color: #0000bb;\">file_get_contents<\/span><span style=\"color: #007700;\">(<\/span><span style=\"color: #dd0000;\">&#8220;sample.xml&#8221;<\/span><span style=\"color: #007700;\">));<br \/>\n<\/span>echo\u00a0<span style=\"color: #0000bb;\">$a<\/span><span style=\"color: #007700;\">-&gt;<\/span><span style=\"color: #0000bb;\">example<\/span><span style=\"color: #007700;\">[<\/span><span style=\"color: #0000bb;\">0<\/span><span style=\"color: #007700;\">];<br \/>\n<\/span><span style=\"color: #0000bb;\">?&gt;<br \/>\n<\/span><br \/>\nThe example above will read the sample XML file above and will output the contents of the first &lt;example&gt; element.<\/p>\n<p><strong>Writing a XML file<\/strong><\/p>\n<p>&lt;?php<br \/>\n$a = new SimpleXMLElement(&#8216;&lt;?xml version=&#8221;1.0&#8243; ?&gt;&lt;root&gt;&lt;\/root&gt;&#8217;);<br \/>\nfor($i = 1; $i &lt;= 9; $i++) {<br \/>\n$a-&gt;addChild(&#8220;element$i&#8221;,&#8221;Hello&#8221;);<br \/>\n}<br \/>\necho $a-&gt;asXML();<br \/>\n?&gt;<\/p>\n<p>The above example will create the following XML file:<\/p>\n<p>&lt;?xml version=&#8221;1.0&#8243;?&gt;<br \/>\n&lt;root&gt;&lt;element1&gt;Hello&lt;\/element1&gt;&lt;element2&gt;Hello&lt;\/element2&gt;&lt;element3&gt;Hello&lt;\/element3&gt;&lt;element4&gt;Hello&lt;\/element4&gt;&lt;element5&gt;Hello&lt;\/element5&gt;&lt;element6&gt;Hello&lt;\/element6&gt;&lt;element7&gt;Hello&lt;\/element7&gt;&lt;element8&gt;Hello&lt;\/element8&gt;&lt;element9&gt;Hello&lt;\/element9&gt;&lt;\/root&gt;<\/p>\n<p>So as you can see, you can very easily manipulate XML with SimpleXML. Don&#8217;t forget to subscribe to Compdigitec Labs for more interesting articles!<\/p>","protected":false},"excerpt":{"rendered":"<p>If you ever do get the need to manipulate (read\/write) XML, you could try using the PHP DOM API, but it&#8217;s very difficult to use the DOM. An good extension-less way to access and manipulate XML instead is SimpleXML. It&#8217;s very easy to use compared to DOM and features a easy way to access attributes [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[15,5],"tags":[197,864,202,201,196,200,199,198],"_links":{"self":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts\/41"}],"collection":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/comments?post=41"}],"version-history":[{"count":0,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts\/41\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/media?parent=41"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/categories?post=41"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/tags?post=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}