Home > Backend Development > PHP Tutorial > php对xml的操作十分简单,

php对xml的操作十分简单,

WBOY
Release: 2016-06-13 11:55:58
Original
972 people have browsed it

php对xml的操作非常简单,急急急

http://api.map.baidu.com/telematics/v3/weather?location=北京&output=xml&ak=gfPnxaaiPyNIhM7c3eZ1kmEL
Copy after login

这个得到XML 如何把currentCity节点的值赋给$city,循环检索weather_data下的所有节点,赋值给数组 -》$date ,给$dayPictureUrl。
------解决方案--------------------
$url = 'http://api.map.baidu.com/telematics/v3/weather?location=北京&output=xml&ak=gfPnxaaiPyNIhM7c3eZ1kmEL';
$xml = simplexml_load_file($url);

echo $city = $xml->xpath('//currentCity')[0];
foreach($xml->xpath('//weather_data') as $r) {
$date = (array)$r->date;
$dayPictureUrl = (array)$r->dayPictureUrl;
}
var_dump($city, $date, $dayPictureUrl);
Copy after login
北京object(SimpleXMLElement)#2 (0) {<br />}<br />array(4) {<br />  [0]=><br />  string(30) "周五(今天, 实时:24℃)"<br />  [1]=><br />  string(6) "周六"<br />  [2]=><br />  string(6) "周日"<br />  [3]=><br />  string(6) "周一"<br />}<br />array(4) {<br />  [0]=><br />  string(57) "http://api.map.baidu.com/images/weather/day/leizhenyu.png"<br />  [1]=><br />  string(51) "http://api.map.baidu.com/images/weather/day/yin.png"<br />  [2]=><br />  string(52) "http://api.map.baidu.com/images/weather/day/qing.png"<br />  [3]=><br />  string(52) "http://api.map.baidu.com/images/weather/day/qing.png"<br />}<br /><br />
Copy after login

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template