如何用php解析xml

如何用php解析xml,php,drupal,drupal-6,Php,Drupal,Drupal 6,我正试图从雅虎网站上阅读天气信息。 使用下面的代码,我能够打印xml。 我现在真正想要实现的是把温度和图像放在两个不同的变量中 $zipCode = "44418"; $url = "http://weather.yahooapis.com/forecastrss"; $zip = "?w=$zipCode"; $fullUrl = $url . $zip.'&u=c'; $curlObject = curl_init(); curl_setopt($curlObject,C

我正试图从雅虎网站上阅读天气信息。 使用下面的代码,我能够打印xml。 我现在真正想要实现的是把温度和图像放在两个不同的变量中

 $zipCode = "44418";
 $url = "http://weather.yahooapis.com/forecastrss";
 $zip = "?w=$zipCode";
 $fullUrl = $url . $zip.'&u=c';
 $curlObject = curl_init();
 curl_setopt($curlObject,CURLOPT_URL,$fullUrl);
 curl_setopt($curlObject,CURLOPT_HEADER,false);
 curl_setopt($curlObject,CURLOPT_RETURNTRANSFER,true);
 $returnYahooWeather = curl_exec($curlObject);
 curl_close($curlObject);
 print "yahooWeather". $returnYahooWeather;

//$temperature 
//$image
我发现使用PHP是最容易的

$xml = simplexml_load_string($returnYahooWeather);
echo $xml->Path->To->Temperature;
这很简单,您可以将XPath与SimpleXML一起使用:)。还有其他解析XML的方法,正如前面提到的,DOMDocument就是其中之一。

我发现用PHP解析XML最容易

$xml = simplexml_load_string($returnYahooWeather);
echo $xml->Path->To->Temperature;

这很简单,您可以将XPath与SimpleXML一起使用:)。还有其他解析XML的方法,正如前面提到的,DOMDocument就是其中之一。

您应该继续使用or解析XML,然后可以迭代结果。使用SimpleXML时,如下所示:

$zipCode = "44418";
$url = "http://weather.yahooapis.com/forecastrss";
$zip = "?w=$zipCode";
$fullUrl = $url . $zip.'&u=c';
$curlObject = curl_init();
curl_setopt($curlObject,CURLOPT_URL,$fullUrl);
curl_setopt($curlObject,CURLOPT_HEADER,false);
curl_setopt($curlObject,CURLOPT_RETURNTRANSFER,true);
$returnYahooWeather = curl_exec($curlObject);
curl_close($curlObject);
//print "here". $returnYahooWeather;

$xmlobj=simplexml_load_string($returnYahooWeather);

$res = $xmlobj->xpath("//yweather:condition");
$tmp = false;
while(list( , $node) = each($res)) {
  $tmp = $node;
 }
$attribs = $tmp->attributes();
print "Temperature [".$attribs['temp']."]";

您应该继续使用或来解析XML,然后可以对结果进行迭代。使用SimpleXML时,如下所示:

$zipCode = "44418";
$url = "http://weather.yahooapis.com/forecastrss";
$zip = "?w=$zipCode";
$fullUrl = $url . $zip.'&u=c';
$curlObject = curl_init();
curl_setopt($curlObject,CURLOPT_URL,$fullUrl);
curl_setopt($curlObject,CURLOPT_HEADER,false);
curl_setopt($curlObject,CURLOPT_RETURNTRANSFER,true);
$returnYahooWeather = curl_exec($curlObject);
curl_close($curlObject);
//print "here". $returnYahooWeather;

$xmlobj=simplexml_load_string($returnYahooWeather);

$res = $xmlobj->xpath("//yweather:condition");
$tmp = false;
while(list( , $node) = each($res)) {
  $tmp = $node;
 }
$attribs = $tmp->attributes();
print "Temperature [".$attribs['temp']."]";

那么你是在马上打折你的最佳解决方案(a)?听起来很有希望。我同意下面的答案,使用simplexml或DOM应该可以做到这一点。但您也不应该从预处理函数内部打印/回显。相反,创建一个可以在模板中使用的变量。例如,
$variables['temperature']=TEMP code在此
那么在模板中,您只需执行
@Laxman13,我知道这一点,谢谢。在转到tpl.php文件之前,您只需要先看看发生了什么:)那么您马上就打折了您的最佳解决方案(a)?听起来很有希望。我同意下面的答案,使用simplexml或DOM应该可以做到这一点。但您也不应该从预处理函数内部打印/回显。相反,创建一个可以在模板中使用的变量。例如,
$variables['temperature']=TEMP code在此
那么在模板中,您只需执行
@Laxman13,我知道这一点,谢谢。在转到tpl.php文件之前,只需要先看看发生了什么:)谢谢,它可以工作,但我不理解在(list(,$node)=each($res))时循环中正在做什么{..如果你不介意的话,请解释一下。这只是我的懒惰。如果有多个
yweather:condition
元素,那么它会对所有元素进行迭代。在这种情况下,如果只有1个元素,它只会得到那一项。我确信有一种方法可以不用循环,但这会更快,因为我有代码。不是吗谢谢,它可以工作,但我不明白在循环中(list(,$node)=each($res))做了什么{..如果你不介意的话,请解释一下。这只是我的懒惰。如果有多个
yweather:condition
元素,那么它会对所有元素进行迭代。在这种情况下,如果只有1个元素,它只会得到那一项。我确信有一种方法可以不用那个循环,但这会更快,因为我有代码。