
如何用php读取xml内容并并放入数组
xml内容如下:<CityWeatherResponse><status>success</status><currentCity>北京</currentCity><re...
xml内容如下:
<CityWeatherResponse>
<status>success</status>
<currentCity>北京</currentCity>
<results>
<result><date>今天(周日)</date><dayPictureUrl>http://t12.baidu.com/it/u=491053973,3965119301&fm=58</dayPictureUrl><nightPictureUrl>http://t10.baidu.com/it/u=914568999,1204794686&fm=58</nightPictureUrl><weather>多云</weather><wind>微风</wind><temperature>32~23℃</temperature></result>
<result><date>明天(周一)</date><dayPictureUrl>http://t10.baidu.com/it/u=1454578626,1487058329&fm=58</dayPictureUrl><nightPictureUrl>http://t12.baidu.com/it/u=3048263366,2821848545&fm=58</nightPictureUrl><weather>阴转雷阵雨</weather><wind>微风</wind><temperature>31~24℃</temperature></result>
<result><date>后天(周二)</date><dayPictureUrl>http://t10.baidu.com/it/u=1454578626,1487058329&fm=58</dayPictureUrl><nightPictureUrl>http://t11.baidu.com/it/u=2439999487,3475991843&fm=58</nightPictureUrl><weather>阴转晴</weather><wind>微风</wind><temperature>31~23℃</temperature></result>
</results>
</CityWeatherResponse>
以上xml数据来自于网络服务
我用
$string=file_get_contents($url);//$url就是上面的xml内容
$xml=simplexml_load_string($string);
$results=$xml->results;
可以读取整个results,但是在results中,有三个result,我想读取其中某一个就不会了,比如我只想读取其中第二个result的数据,该怎么实现?不要正则,谢谢! 展开
<CityWeatherResponse>
<status>success</status>
<currentCity>北京</currentCity>
<results>
<result><date>今天(周日)</date><dayPictureUrl>http://t12.baidu.com/it/u=491053973,3965119301&fm=58</dayPictureUrl><nightPictureUrl>http://t10.baidu.com/it/u=914568999,1204794686&fm=58</nightPictureUrl><weather>多云</weather><wind>微风</wind><temperature>32~23℃</temperature></result>
<result><date>明天(周一)</date><dayPictureUrl>http://t10.baidu.com/it/u=1454578626,1487058329&fm=58</dayPictureUrl><nightPictureUrl>http://t12.baidu.com/it/u=3048263366,2821848545&fm=58</nightPictureUrl><weather>阴转雷阵雨</weather><wind>微风</wind><temperature>31~24℃</temperature></result>
<result><date>后天(周二)</date><dayPictureUrl>http://t10.baidu.com/it/u=1454578626,1487058329&fm=58</dayPictureUrl><nightPictureUrl>http://t11.baidu.com/it/u=2439999487,3475991843&fm=58</nightPictureUrl><weather>阴转晴</weather><wind>微风</wind><temperature>31~23℃</temperature></result>
</results>
</CityWeatherResponse>
以上xml数据来自于网络服务
我用
$string=file_get_contents($url);//$url就是上面的xml内容
$xml=simplexml_load_string($string);
$results=$xml->results;
可以读取整个results,但是在results中,有三个result,我想读取其中某一个就不会了,比如我只想读取其中第二个result的数据,该怎么实现?不要正则,谢谢! 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询