{"id":635,"date":"2021-11-14T20:57:16","date_gmt":"2021-11-15T01:57:16","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=635"},"modified":"2024-02-08T08:44:19","modified_gmt":"2024-02-08T13:44:19","slug":"using-php-and-simplexml-to-manipulate-xml","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/using-php-and-simplexml-to-manipulate-xml\/","title":{"rendered":"Using PHP and SimpleXML to manipulate XML"},"content":{"rendered":"\n<p>SimpleXML is a SAX XML parser. It represents an XML document as a hierarchical set of objects and properties where each element is an object and each attribute is an object property.<\/p>\n\n\n\n<p><strong>Access an XML element using SimpleXML<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?xml version='1.0'?&gt;\n&lt;hotels&gt;\n   &lt;hotel stars=\"5\"&gt;\n       &lt;name&gt;President Wilson&lt;\/name&gt;\n       &lt;city&gt;Geneva&lt;\/city&gt;\n       &lt;country&gt;Switzerland&lt;\/country&gt;\n   &lt;\/hotel&gt;\n&lt;\/hotels&gt;\n<\/code><\/pre>\n\n\n\n<p>Save this file as hotel.xml<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ load XML file\n$xml = simplexml_load_file('hotel.xml') or die (\"Unable to load XML\");\n\n\/\/ access xml element\necho 'Hotel: ' . $xml-&gt;hotel-&gt;name;\n<\/code><\/pre>\n\n\n\n<p>Note that we did not have to call the tag &#8220;hotels&#8221; since it is the root.<\/p>\n\n\n\n<p><strong>Accessing multiple XML elements using SimpleXML<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?xml version='1.0'?&gt;\n&lt;hotels&gt;\n   &lt;hotel stars=\"5\"&gt;\n       &lt;name&gt;President Wilson&lt;\/name&gt;\n       &lt;city&gt;Geneva&lt;\/city&gt;\n       &lt;country&gt;Switzerland&lt;\/country&gt;\n   &lt;\/hotel&gt;\n   &lt;hotel stars=\"5\"&gt;\n       &lt;name&gt;Montreux Palace&lt;\/name&gt;\n       &lt;city&gt;Montreux&lt;\/city&gt;\n       &lt;country&gt;Switzerland&lt;\/country&gt;\n   &lt;\/hotel&gt;\n   &lt;hotel stars=\"3\"&gt;\n       &lt;name&gt;Express by Holiday Inn&lt;\/name&gt;\n       &lt;city&gt;Geneva&lt;\/city&gt;\n       &lt;country&gt;Switzerland&lt;\/country&gt;\n   &lt;\/hotel&gt;        \n&lt;\/hotels&gt;\n<\/code><\/pre>\n\n\n\n<p>Save this file as hotels.xml<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ show how to access multiple elements with SimpleXML\n\n\/\/ load XML file\n$xml = simplexml_load_file('hotels.xml') or die (\"Unable to load XML\");\n\n\/\/ access XML elements\nforeach ($xml-&gt;hotel as $hotel) {\n   \/\/ print data\n   print '&lt;br&gt;Hotel: ' . $hotel-&gt;name;\n}\n\n\/\/ count elements\nprint '&lt;br&gt;Total: ' . count($xml-&gt;hotel);\n<\/code><\/pre>\n\n\n\n<p>The foreach loop iterates over the XML elements. count() function allows users to count any type of element.<\/p>\n\n\n\n<p><strong>Accessing attributes of XML elements<\/strong>&nbsp;SimpleXML stores attributes as key value pairs in an associative array.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ accessing attributes of xml elements\n\n\/\/ load XML file\n$xml = simplexml_load_file('hotels.xml') or die (\"Unable to load XML\");\n\n\/\/ access XML elements\nforeach ($xml-&gt;hotel as $hotel) {\n   \/\/ print data\n   print '&lt;br&gt;' . $hotel-&gt;name . ' is a ' . $hotel&#91;'stars'] . ' star hotel';\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Altering XML element and attribute values<\/strong>&nbsp;This simple example shows how to change XML element and attribute values:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ alter xml elements and attributes using SimpleXML\n\n\/\/ load XML file\n$xml = simplexml_load_file('hotels.xml') or die (\"Unable to load XML\");\n\n\/\/ change element value\n$xml-&gt;hotel&#91;2]-&gt;name = 'Crown Plaza';\n\n\/\/ change attribute value \n$xml-&gt;hotel&#91;2]{'stars'} = 4;\n\n\/\/ print new XML string\nheader('Content-Type: text\/html');\necho $xml-&gt;asXML();\n<\/code><\/pre>\n\n\n\n<p>The asXML() function converts SimpleXML object tree to XML. Following is the output of this program<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?xml version=\"1.0\"?&gt;\n&lt;hotels&gt;\n   &lt;hotel stars=\"5\"&gt;\n       &lt;name&gt;President Wilson&lt;\/name&gt;\n       &lt;city&gt;Geneva&lt;\/city&gt;\n       &lt;country&gt;Switzerland&lt;\/country&gt;\n   &lt;\/hotel&gt;\n   &lt;hotel stars=\"5\"&gt;\n       &lt;name&gt;Montreux Palace&lt;\/name&gt;\n       &lt;city&gt;Montreux&lt;\/city&gt;\n       &lt;country&gt;Switzerland&lt;\/country&gt;\n   &lt;\/hotel&gt;\n   &lt;hotel stars=\"4\"&gt;\n       &lt;name&gt;Crown Plaza&lt;\/name&gt;\n       &lt;city&gt;Geneva&lt;\/city&gt;\n       &lt;country&gt;Switzerland&lt;\/country&gt;\n   &lt;\/hotel&gt;        \n<\/code><\/pre>\n\n\n\n<p>Note that this program only printed an edited XML string to screen, it did not edit the XML file. To edit the XML file, you need to rewrite the entire file.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SimpleXML is a SAX XML parser. It represents an XML document as a hierarchical set of objects and properties where each element is an object and each attribute is an object property. Access an XML element using SimpleXML Save this file as hotel.xml Note that we did not have to call the tag &#8220;hotels&#8221; since [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-635","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/635","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=635"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/635\/revisions"}],"predecessor-version":[{"id":636,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/635\/revisions\/636"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}