Home >Backend Development >PHP Tutorial >How to correctly use PHP XMLReader to parse XML documents_PHP Tutorial

How to correctly use PHP XMLReader to parse XML documents_PHP Tutorial

WBOY
WBOYOriginal
2016-07-15 13:31:47986browse

The code example we are using

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. while ($xml->read()) { >// echo $xml->
  9. name, "==
  10. >", $xml ->
  11. depth, "<br>";
    if (!empty($condition) && !empty($temp_c)) { break; }
  12. if ($xml-
    >
  13. name
  14. == 'condition ' && empty($condition)) {
  15. // Take the first condition
  16. $ condition = $xml-
    >
  17. getAttribute('data');
  18. }
  19. if ($xml->name == 'temp_c' && empty($temp_c)) {
  20. // Get the first temp_c
  21. $temp_c = $xml-
    >
  22. getAttribute('data');
  23. }
  24. $xml- >read();
  25. }
  26. $xml-
  27. > ;close();
  28. echo 'Weather:', $condition, '
  29. <
  30. br />
  31. ';
  32. echo 'Temperature:', $temp_c, '< br />
  33. ';
  34. We just need to use PHP XMLReader to get the first condition and the first temp_c, then traverse all the nodes and write the first condition and the first temp_c encountered into variables , and finally output. http://www.bkjia.com/PHPjc/446186.html
  35. www.bkjia.com

true

http: //www.bkjia.com/PHPjc/446186.html

TechArticle


The code examples we use 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...

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn