Php SimpleXML子元素属性

Php SimpleXML子元素属性,php,xml,parsing,simplexml,Php,Xml,Parsing,Simplexml,要获取温度节点第一次出现的值属性,请执行以下操作: echo '<b>Temperature:</b> '. $xml->forecast->children('temperature')->attributes('value'); 要将考虑在内,请使用xpath: $result = $xml->forecast[0]->time[0]->temperature["value"]; 这将选择所有time节点,现在循环: $resu

要获取
温度
节点第一次出现的
属性,请执行以下操作:

echo '<b>Temperature:</b> '. $xml->forecast->children('temperature')->attributes('value');
要将
考虑在内,请使用
xpath

$result = $xml->forecast[0]->time[0]->temperature["value"];
这将选择所有
time
节点,现在循环:

$results = $xml->xpath("//time");
foreach($results as$result)
echo“从$result[from]到$result[to]的温度:{$result->temperature['value']}
”;

查看它的工作情况:

问题是,您在寻找哪种温度?XML文档中的第一个值最简单的方法是一点
XPath
$XML->XPath('forecast/time[1]/temperature/@value')
。请在您的问题中添加一段XML,链接可能有一天会消失,那么您的问题就没有任何意义了。
$results = $xml->xpath("//time");
foreach ($results as $result)
    echo "temperature for $result[from] to $result[to]: {$result->temperature['value']}<br />";