我們在運用
PHP XMLReader的程式碼範例如下:
- < ?PHP
- header("Content-type:text/html;
Charset=utf-8");
- $url = "http://www.google.com/
ig/api?weather=shenzhen";
- // 載入XML內容
- $xml = new XMLReader();
- $xml->open($url);
- $condition = '';
- $temp_c = '';
- while ($xml->read()) {
- // echo $xml->name, "==>",
$xml->depth, "<br>";
- if (!empty($condition)
&& !empty($temp_c)) {
- break;
- }
- if ($xml->name == 'condition'
&& empty($condition)) {
- // 取第一個condition
- $condition = $xml->getAttribute('data');
- }
- if ($xml->name == 'temp_c' &&
empty($temp_c)) {
- // 取第一個temp_c
- $temp_c = $xml->getAttribute('data');
- }
- $xml->read();
- }
- $xml->close();
- echo '天氣:', $condition, '< br />';
- 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內容...