如何访问XML元素中的属性

时间:2017-02-10 13:47:48

标签: php xml

我正在使用雅虎天气API来获取我所在地区的天气。我从API的调用中得到了一个XML文档。 我尝试了以下方法来访问元素中的属性:yweather:condition'没有成功。我正试图访问“临时”#在这个元素中。我收到以下错误:

  

'语法错误,意外':'排队......

 $endpoint = 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%3D%20560743&diagnostics=true';   
 $xml = simplexml_load_file($endpoint) or die('failed to load xml');
 $out = $xml -> diagnostics ->  yweather:condition['temp'];
 echo $out;


 <query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:count="1" yahoo:created="2017-02-10T11:54:56Z" yahoo:lang="en-US">
    <diagnostics>
       <publiclyCallable>true</publiclyCallable>
       <url execution-start-time="1" execution-stop-time="7" execution-time="6">...</url>
       <user-time>8</user-time>
       <service-time>6</service-time>
       <build-version>2.0.92</build-version>
       <yweather:condition xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="26" date="Fri, 10 Feb 2017 11:00 AM GMT" temp="37" text="Cloudy"/>
    </diagnostics>
    <results>...</results>
</query>

0 个答案:

没有答案