If you plan to process XML data in PHP, you need an XML library to extract data for you. For example, parse RSS feed or pattern match (search for XHTML images or elements ).
SimpleXML extension provides a very intuitive API to easily convert XML into objects and traverse elements. The only drawback is to load the entire document or a very large XML file in the memory. Its performance may be a problem.
If performance is a factor, you can use XMLReader. XMLReader is an XML parser that traverses each node during the loading process, instead of loading the entire document in the memory.
The following code uses simple XML to obtain the latest RSS from my website. On my server, I have used the Curl library to process HTTP connections because it supports server hosting and is more secure.
- <?php
-
- function load_file($url) {
- $ch = curl_init($url);
- #Return http response in string
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $xml = simplexml_load_string(curl_exec($ch));
- return $xml;
- }
-
- $feedurl = 'http://naveenbalani.com/index.php/feed/';
- $rss = load_file($feedurl);
-
- foreach ($rss->channel->item as $item) {
- echo "
- echo "<p>" . $item->description . "</p>";
- }
-
- ?>
Http://css.dzone.com/news/parsing-xml-data-php