我試圖用PHP解析XML,但是我遇到了問題。解析嵌套XML節點的屬性值
所以XML看起來像這樣(簡體)
<weatherdata>
<forecast>
<tabular>
<time from="2015-09-25T20:00:00" to="2015-09-25T23:00:00" period="3">
<symbol number="4" numberEx="4" name="Cloudy" var="04" />
</time>
<time from="2015-09-25T23:00:00" to="2015-09-26T05:00:00" period="0">
<symbol number="9" numberEx="46" name="Light rain" var="46" />
</time>
.....
我能夠從值加載所有的時間>是這樣的:
foreach($xml->forecast->tabular as $forecastItem){
$attr = $forecastItem->attributes();
$froms[] = $attr['from'];
}
但後來我試圖加載這樣的符號的名稱屬性:
foreach($xml->forecast->tabular as $forecastItem){
$attr = $forecastItem->attributes();
$froms[] = $attr['from'];
$attr2 = $forecastItem->symbol->attributes();
$names[] = $attr2['name'];
}
它顯示我main()節點不存在的錯誤。基本上我想要做的就是將所有名稱加載到一個數組中,就像來自froms一樣。
@Alan謝謝,我剛剛也注意到了,好像我們在同一時間編輯它。 –
沒問題,重要的是,它的現予以更正。 :) –