1

嗨,我想得到 node that contains the text 116.. but what I am trying is not working. Any help would be greatly appreciated!!

XML (codes.xml):

<codes>
<condition>
<code>116</code>
<description>Moderate or heavy snow in area with thunder</description>
<day_icon>wsymbol_0012_heavy_snow_showers</day_icon>
<night_icon>wsymbol_0028_heavy_snow_showers_night</night_icon>
</condition>
<condition>
<code>392</code>
<description>Patchy light snow in area with thunder</description>
<day_icon>wsymbol_0016_thundery_showers</day_icon>
<night_icon>wsymbol_0032_thundery_showers_night</night_icon>
</condition>
</codes>

My PHP CODE TO CALL:

$codes = simplexml_load_file('xml/codes.xml');

foreach($codes->codes->condition AS $match){
    if($match->code == "116")
    {echo $match->description;}
    };
4

2 回答 2

1

$codes是根节点,不是$codes->codes

foreach($codes->condition AS $match)
{
    if($match->code == "116")
    {
        echo $match->description;
    }
};
于 2012-04-30T16:13:30.417 回答
0

在大多数情况下,您可以使用 xpath 来执行此操作。更容易阅读和处理复杂的搜索。

$codes = simplexml_load_file('xml/codes.xml');
$nodes = $codes->xpath("/codes/condition[code=116]");
foreach ($nodes as $N)
  echo $N->description;

有关 xpath 语法的更多信息,请阅读http://www.w3schools.com/xpath/xpath_syntax.asp

于 2012-04-30T16:36:46.747 回答