How to correctly use PHP XmlReader parsing XML document _php Tutorial

Source: Internet
Author: User
We are using

The code examples for PHP XmlReader are as follows:

  1. < ? PHP
  2. Header ("content-type:text/html;
    Charset=utf-8 ");
  3. $ URL = "http://www.google.com/
    Ig/api?weather=shenzhen ";
  4. Loading XML content
  5. $ XML = New XMLReader ();
  6. $xml- > open ($url);
  7. $ condition = '' ;
  8. $ Temp_c = '' ;
  9. While ($xml->Read ()) {
  10. //Echo $xml- > name, "= = > ",
    $xml->depth, "<br>";
  11. if (!empty ($condition)
    &&!empty ($temp _c)) {
  12. Break
  13. }
  14. if ($xml->name = = ' condition '
    && empty ($condition)) {
  15. Take the first one condition
  16. $ condition = $xml- > getattribute (' data ');
  17. }
  18. if ($xml->name = = ' Temp_c ' &&
    Empty ($temp _c)) {
  19. Take the first one Temp_c
  20. $ Temp_c = $xml- > getattribute (' data ');
  21. }
  22. $xml- > read ();
  23. }
  24. $xml- > close ();
  25. echo ' weather: ', $condition, ' < br />';
  26. Echo ' temp: ', $temp _c, ' < br />';

We just need to use PHP XmlReader to take the first condition and the first temp_c, so traverse all the nodes, will encounter the first condition and the first Temp_c write variable, the last output.


http://www.bkjia.com/PHPjc/446186.html www.bkjia.com true http://www.bkjia.com/PHPjc/446186.html techarticle The code examples we use in PHP XmlReader are as follows:? PHP header (content-type:text/html; Charset = Utf-8); $ url = http://www.google.com/ig/api?weather=shenzhen; Load XML content ...

  • Related Article

    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.