如何正確運用PHP XMLReader解析XML文檔_PHP教程

來源:互聯網
上載者:User
我們在運用

PHP XMLReader的程式碼範例如下:

  1. < ?PHP
  2. header("Content-type:text/html;
    Charset=utf-8");
  3. $url = "http://www.google.com/
    ig/api?weather=shenzhen";
  4. // 載入XML內容
  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. // 取第一個condition
  16. $condition = $xml->getAttribute('data');
  17. }
  18. if ($xml->name == 'temp_c' &&
    empty($temp_c)) {
  19. // 取第一個temp_c
  20. $temp_c = $xml->getAttribute('data');
  21. }
  22. $xml->read();
  23. }
  24. $xml->close();
  25. echo '天氣:', $condition, '< br />';
  26. echo '溫度:', $temp_c, '< br />';

我們只是需要運用PHP XMLReader取第一個condition和第一個temp_c,於是遍曆所有的節點,將遇到的第一個condition和第一個temp_c寫入變數,最後輸出。


http://www.bkjia.com/PHPjc/446186.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/446186.htmlTechArticle我們在運用 PHP XMLReader的程式碼範例如下: ?PHP header(Content-type:text/html; Charset = utf -8); $ url = http://www.google.com/ ig/api?weather=shenzhen ; //載入XML內容...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.