{"id":1529,"date":"2011-02-04T23:19:24","date_gmt":"2011-02-04T17:49:24","guid":{"rendered":"http:\/\/naveenbalani.com\/?p=1529"},"modified":"2017-03-24T18:07:09","modified_gmt":"2017-03-24T12:37:09","slug":"parsing-xml-data-in-php","status":"publish","type":"post","link":"https:\/\/navveenbalani.dev\/index.php\/articles\/parsing-xml-data-in-php\/","title":{"rendered":"Parsing XML data in PHP"},"content":{"rendered":"<p>If you are planning to deal with XML data in PHP , for instance parsing an RSS feed or pattern matching (finding images or elements in an XHTML) , than probably you need an XML library which extracts the data for you.<\/p>\n<p>The <a href=\"http:\/\/php.net\/manual\/en\/book.simplexml.php\">SimpleXML <\/a>extension provides a very intuitive API to convert XML to an object and traverse elements easily.The only disadvantage is that it loads entire document in memory, so for very large XML files, performance could be an issue.<!--more--><\/p>\n<p>If performance is a consideration, you can go with <a href=\"http:\/\/www.php.net\/manual\/en\/intro.xmlreader.php\">XMLReader<\/a> , which is an XML pull parser, which doesn&#8217;t load entire document in memory, instead it traverses each node on the way.<\/p>\n<p>There are many special purpose libraries available, like <a href=\"http:\/\/simplepie.org\/\">simple pie<\/a> (which is used by wordpress internally for RSS feeds) but I found it too overloaded while dealing with simple fields and didn&#8217;t offer XPATH capability to get required nodes quickly and you end up dealing with pattern matching capability with simple pie.<br \/>\nThe following code uses simple xml and fetches the latest RSS feeds from my website. I have used curl library to deal with http connections, as its more secured and supported on my hosting server.<\/p>\n<pre>\r\n&lt;?php\r\n\r\nfunction load_file($url) {\r\n$ch = curl_init($url);\r\n#Return http response in string\r\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\r\n$xml = simplexml_load_string(curl_exec($ch));\r\nreturn $xml;\r\n}\r\n\r\n$feedurl = 'http:\/\/naveenbalani.com\/index.php\/feed\/';\r\n$rss = load_file($feedurl);\r\n\r\nforeach ($rss-&gt;channel-&gt;item as $item) {\r\necho \"&lt;h2&gt;\" . $item-&gt;title . \"&lt;\/h2&gt;\";\r\necho \"&lt;p&gt;\" . $item-&gt;description . \"&lt;\/p&gt;\";\r\n}\r\n\r\n?&gt;\r\n<\/pre>\n<p>&nbsp;<\/p>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 538px; width: 1px; height: 1px; overflow: hidden;\">The signer may need to be added to local trust store &#8220;C:\/W75\/java\/jre\/lib\/security\/cacerts&#8221;<br \/>\nlocated in SSL configuration alias &#8220;DefaultSystemProperties<\/div>\n","protected":false},"excerpt":{"rendered":"<p>If you are planning to deal with XML data in PHP , for instance parsing an RSS feed or pattern matching (finding images or elements in an XHTML) , than probably you need an XML library which extracts the data for you. The SimpleXML extension provides a very intuitive API to convert XML to an [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2128,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,151],"tags":[152,153],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.0.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Parsing XML data in PHP - Current and Future Technology Trends by Navveen Balani<\/title>\n<meta name=\"description\" content=\"Parsing XML data in PHP -\" \/>\n<link rel=\"canonical\" href=\"https:\/\/navveenbalani.dev\/index.php\/articles\/parsing-xml-data-in-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Parsing XML data in PHP - Current and Future Technology Trends by Navveen Balani\" \/>\n<meta property=\"og:description\" content=\"Parsing XML data in PHP -\" \/>\n<meta property=\"og:url\" content=\"https:\/\/navveenbalani.dev\/index.php\/articles\/parsing-xml-data-in-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Current and Future Technology Trends by Navveen Balani\" \/>\n<meta property=\"article:published_time\" content=\"2011-02-04T17:49:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-03-24T12:37:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/navveenbalani.dev\/wp-content\/uploads\/2016\/09\/bk6.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"450\" \/>\n\t<meta property=\"og:image:height\" content=\"374\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"1 minute\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/navveenbalani.dev\/#website\",\"url\":\"https:\/\/navveenbalani.dev\/\",\"name\":\"Current and Future Technology Trends by Navveen Balani\",\"description\":\"Current and Future Technology Trends by Navveen Balani\",\"publisher\":{\"@id\":\"https:\/\/navveenbalani.dev\/#\/schema\/person\/51f7ab14b20611d95e3c7fd4ea0950bf\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/navveenbalani.dev\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/parsing-xml-data-in-php\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/navveenbalani.dev\/wp-content\/uploads\/2016\/09\/bk6.jpg\",\"width\":450,\"height\":374},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/parsing-xml-data-in-php\/#webpage\",\"url\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/parsing-xml-data-in-php\/\",\"name\":\"Parsing XML data in PHP - Current and Future Technology Trends by Navveen Balani\",\"isPartOf\":{\"@id\":\"https:\/\/navveenbalani.dev\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/parsing-xml-data-in-php\/#primaryimage\"},\"datePublished\":\"2011-02-04T17:49:24+00:00\",\"dateModified\":\"2017-03-24T12:37:09+00:00\",\"description\":\"Parsing XML data in PHP -\",\"breadcrumb\":{\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/parsing-xml-data-in-php\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/navveenbalani.dev\/index.php\/articles\/parsing-xml-data-in-php\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/parsing-xml-data-in-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/navveenbalani.dev\/\",\"url\":\"https:\/\/navveenbalani.dev\/\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":2,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/parsing-xml-data-in-php\/\",\"url\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/parsing-xml-data-in-php\/\",\"name\":\"Parsing XML data in PHP\"}}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/parsing-xml-data-in-php\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/parsing-xml-data-in-php\/#webpage\"},\"author\":{\"@id\":\"https:\/\/navveenbalani.dev\/#\/schema\/person\/51f7ab14b20611d95e3c7fd4ea0950bf\"},\"headline\":\"Parsing XML data in PHP\",\"datePublished\":\"2011-02-04T17:49:24+00:00\",\"dateModified\":\"2017-03-24T12:37:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/parsing-xml-data-in-php\/#webpage\"},\"commentCount\":4,\"publisher\":{\"@id\":\"https:\/\/navveenbalani.dev\/#\/schema\/person\/51f7ab14b20611d95e3c7fd4ea0950bf\"},\"image\":{\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/parsing-xml-data-in-php\/#primaryimage\"},\"keywords\":\"php,php xml\",\"articleSection\":\"Articles,PHP\",\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/navveenbalani.dev\/#\/schema\/person\/51f7ab14b20611d95e3c7fd4ea0950bf\",\"name\":\"Navveen\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/navveenbalani.dev\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/navveenbalani.dev\/wp-content\/uploads\/2019\/07\/navveen_balani.jpeg\",\"width\":200,\"height\":200,\"caption\":\"Navveen\"},\"logo\":{\"@id\":\"https:\/\/navveenbalani.dev\/#personlogo\"},\"sameAs\":[\"http:\/\/naveenbalani.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/posts\/1529"}],"collection":[{"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/comments?post=1529"}],"version-history":[{"count":9,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/posts\/1529\/revisions"}],"predecessor-version":[{"id":2390,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/posts\/1529\/revisions\/2390"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/media\/2128"}],"wp:attachment":[{"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/media?parent=1529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/categories?post=1529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/tags?post=1529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}