How to correctly use PHPXMLReader to parse XML document _ PHP Tutorial

Source: Internet
Author: User
Correct use of PHPXMLReader to parse XML documents. The sample code using PHPXMLReader is as follows :? PHPheader (Content-type: texthtml; Charsetutf-8); $ urlwww. google. comigapi? Weathershenzhen; we are using XML content loading.

The code example of PHP XMLReader is as follows:

  1. <? PHP
  2. Header ("Content-type: text/html;
    Charset = utf-8 ");
  3. $ Url = "http://www.google.com/
    Ig/api? Weather = shenzhen ";
  4. // Load 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 ,"
    ";
  11. If (! Empty ($ condition)
    &&! Empty ($ temp_c )){
  12. Break;
  13. }
  14. If ($ xml-> name = 'condition'
    & Amp; empty ($ condition )){
  15. // Obtain the first condition
  16. $ Condition = $ xml-> getAttribute ('data ');
  17. }
  18. If ($ xml-> name = 'temp _ c '&&
    Empty ($ temp_c )){
  19. // Obtain the first temp_c
  20. $ Temp_c = $ xml-> getAttribute ('data ');
  21. }
  22. $ Xml-> read ();
  23. }
  24. $ Xml-> close ();
  25. Echo 'weather: ', $ condition,' <br/> ';
  26. Echo 'temperature: ', $ temp_c,' <br/> ';

We just need to use PHP XMLReader to get the first condition and the first temp_c, so we traverse all the nodes, write the first condition and the first temp_c into the variable, and finally output the result.


The following is an example of the PHP XMLReader code :? PHP header (Content-type: text/html; Charset = utf-8); $ url = http://www.google.com/ig/api? Weather = shenzhen; // load XML content...

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.