php對xml的操作非常簡單,急

來源:互聯網
上載者:User
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 ';'

My Code都是經實測驗證的!
不然也貼不出結果來

My Code都是經實測驗證的!
不然也貼不出結果來

不要發怒嘛,我這邊就是這個錯誤啊,貌似是括弧有問題

My Code都是經實測驗證的!
不然也貼不出結果來

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℃) )
應該周六,周日都出來的啊
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.