The code example we are using
PHP XMLReader is as follows:
- < ?PHP
- header("Content-type:text/html;
Charset=utf-8"); - $url = "http: //www.google.com/
ig/api?weather=shenzhen"; - // Load XML content
- $xml = new XMLReader();
- $ xml->open($url);
-
$condition = ''; 🎜>;
- while ($xml->read()) { >// echo $xml->
name, "==-
>", $xml ->
- depth, "<br>";
if (!empty($condition) && !empty($temp_c)) { break; }
- if ($xml-
>
name-
== 'condition ' && empty($condition)) {
- // Take the first condition
- $ condition = $xml-
>
getAttribute('data'); -
} -
if ($xml->name == 'temp_c' && empty($temp_c)) {
- // Get the first temp_c
- $temp_c = $xml-
>
getAttribute('data'); -
} -
$xml- >read();
} -
$xml--
> ;close();
echo 'Weather:', $condition, '- <
- br />
'; -
echo 'Temperature:', $temp_c, '< br />
'; -
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
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...