_php tutorial on parsing XML data in PHP

Source: Internet
Author: User
Tags xml parser server hosting
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.

 
 
  1. PHP
  2. function Load_file ($url) {
  3. $ CH = Curl_init ($url);
  4. #Return HTTP response in string
  5. curl_setopt ($ch, Curlopt_returntransfer, true);
  6. $ XML = simplexml_load_string (Curl_exec ($ch));
  7. return $xml;
  8. }
  9. $ Feedurl = ' http://naveenbalani.com/index.php/feed/' ;
  10. $ RSS = Load_file ($feedurl);
  11. foreach ($rss->channel->Item as $item) {
  12. echo " < H2 > " . $item->title. " h2>";
  13. echo " < P > " . $item->description. " p>";
  14. }
  15. ?>

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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.