Php's xml operations are very simple and urgent.

Source: Internet
Author: User
Php's xml operations are very simple and urgent.
Http://api.map.baidu.com/telematics/v3/weather? Location = Beijing & output = xml & ak = gfPnxaaiPyNIhM7c3eZ1kmEL

In this example, how to assign the currentCity node value to $ city to retrieve all nodes in weather_data cyclically and assign the value to the array? -"$ Date, To $ dayPictureUrl.


Reply to discussion (solution)

Where is the great god?

$ Url = 'http: // api.map.baidu.com/telematics/v3/weather? Location = Beijing & 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 );
Beijing object (SimpleXMLElement) #2 (0) {} array (4) {[0] => string (30) "Friday (today, real-time: 24 ℃) "[1] => string (6)" Saturday "[2] => string (6)" Sunday "[3] => string (6) "Monday"} 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 = Beijing & 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 );
Beijing object (SimpleXMLElement) #2 (0) {} array (4) {[0] => string (30) "Friday (today, real-time: 24 ℃) "[1] => string (6)" Saturday "[2] => string (6)" Sunday "[3] => string (6) "Monday"} 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 is verified by actual tests!
Otherwise, no results will be posted.

My code is verified by actual tests!
Otherwise, no results will be posted.

Don't get angry. this is my mistake. it seems that there is a problem with the brackets.

My code is verified by actual tests!
Otherwise, no results will be posted.

OK. it seems that the data to be written separately cannot be directly echo $ city = $ xml-> xpath ('// currentCity') [0];

Yes.
$ City = $ xml-> xpath ('// currentcity ');
Echo $ city [0];

That's your problem.
Besides, the error information must be fixed. Otherwise, how can someone determine the problem?

Your php version is too low.
$ City = $ xml-> xpath ('// currentcity') [0];
Change
$ City = current ($ xml-> xpath ('// currentcity '));

That's your problem.
Besides, the error information must be fixed. Otherwise, how can someone determine the problem?

Your php version is too low.
$ City = $ xml-> xpath ('// currentcity') [0];
Change
$ City = current ($ xml-> xpath ('// currentcity '));

Cainiao doesn't. sorry, the moderator.

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

Why is there only one row of data written in Beijing?
2014-04-25
Array ([0] => Friday (today, real-time: 24 ℃ ))
It should be Saturday and Sunday.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.