If you plan to work with XML data in PHP, you need an XML library to extract the data for you. For example, parsing an RSS feed or pattern matching (looking for XHTML images or elements).
The simplexml extension provides a very intuitive API that makes it easier to convert XML into objects and traverse elements. The only drawback is that loading an entire document or a very large XML file in memory can be a problem.
If performance is a consideration, you can go with XmlReader. XmlReader is an XML parser that loads the entire document in memory, rather than in RAM, by traversing through each node during the loading process.
The following code uses simple XML to get the latest RSS from my site. On my server, I have used the Curl library to handle 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 " < H2 > " . $item->title. " h2>";
- echo " < P > " . $item->description. " p>";
- }
-
- ?>
http://css.dzone.com/news/parsing-xml-data-php
http://www.bkjia.com/PHPjc/445843.html www.bkjia.com true http://www.bkjia.com/PHPjc/445843.html techarticle If you plan to work with XML data in PHP, you need an XML library to extract the data for you. For example, parsing an RSS feed or pattern matching (looking for XHTML images or elements). The simplexml extension provides a ...