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

php中文网
发布: 2016-06-23 13:58:05
原创
846人浏览过

http://api.map.baidu.com/telematics/v3/weather?location=北京&output=xml&ak=gfPnxaaiPyNIhM7c3eZ1kmEL
登录后复制

这个得到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);
登录后复制
登录后复制
北京object(SimpleXMLElement)#2 (0) {}array(4) {  [0]=>  string(30) "周五(今天, 实时:24℃)"  [1]=>  string(6) "周六"  [2]=>  string(6) "周日"  [3]=>  string(6) "周一"}array(4) {  [0]=>  string(57) "http://api.map.baidu.com/images/weather/day/leizhenyu.png"  [1]=>  string(51) "http://api.map.baidu.com/images/weather/day/yin.png"  [2]=>  string(52) "http://api.map.baidu.com/images/weather/day/qing.png"  [3]=>  string(52) "http://api.map.baidu.com/images/weather/day/qing.png"}
登录后复制
登录后复制

$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);
登录后复制
登录后复制
北京object(SimpleXMLElement)#2 (0) {}array(4) {  [0]=>  string(30) "周五(今天, 实时:24℃)"  [1]=>  string(6) "周六"  [2]=>  string(6) "周日"  [3]=>  string(6) "周一"}array(4) {  [0]=>  string(57) "http://api.map.baidu.com/images/weather/day/leizhenyu.png"  [1]=>  string(51) "http://api.map.baidu.com/images/weather/day/yin.png"  [2]=>  string(52) "http://api.map.baidu.com/images/weather/day/qing.png"  [3]=>  string(52) "http://api.map.baidu.com/images/weather/day/qing.png"}
登录后复制
登录后复制

Parse error: syntax error, unexpected '[', expecting ',' or ';' 

我的代码都是经实测验证的!
不然也贴不出结果来

我的代码都是经实测验证的!
不然也贴不出结果来

不要发怒嘛,我这边就是这个错误啊,貌似是括号有问题

我的代码都是经实测验证的!
不然也贴不出结果来

OK了,貌似要分开写的不能直接echo $city = $xml->xpath('//currentCity')[0];

可以这样
 $city = $xml->xpath('//currentCity');
echo $city[0];

那是你的问题呀
再说,错误信息也得贴全了吧,不然别人如何判断问题所在?

想来是你的 php 版本太低了
$city = $xml->xpath('//currentCity')[0];
改为
$city = current($xml->xpath('//currentCity'));

那是你的问题呀
再说,错误信息也得贴全了吧,不然别人如何判断问题所在?

想来是你的 php 版本太低了
$city = $xml->xpath('//currentCity')[0];
改为
$city = current($xml->xpath('//currentCity'));

菜鸟完全不会啊,还请版主见谅啊

foreach($xml->xpath('//weather_data') as $r) {$date = (array)$r->date;print_r($date);}
登录后复制

这样子写的话为什么只有一行数据北京
2014-04-25
Array ( [0] => 周五(今天, 实时:24℃) ) 
应该周六,周日都出来的啊

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号