2012-04-30 114 views
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;} 
    }; 

回答

1

$codes XML節點是根節點,不$codes->codes

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

這個工作完美......我錯過了一個簡單的事實。謝謝! – liveandream

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

相關問題