我正在使用SimpleXMLElement和xpath嘗試從底部的xml讀取<subcategory><name>
。此代碼工作..但while
循環內的東西看起來有點凌亂,現在我也想得到<subcategory><count>
,並以某種方式配對它適當的<subcategory><name>
。閱讀xml:可以xpath讀取2個字段?
$names = $xml->xpath('/theroot/category/subcategories/subcategory/name/');
while(list(, $node) = each($names)) {
echo $node;
}
我的問題:是否有可能同時仍然使用XPath,因爲它看起來像它可以使工作更容易獲得這種配對?
<theroot>
<category>
<name>Category 1</name>
<subcategories>
<subcategory>
<name>Subcategory 1.1</name>
<count>18</count>
</subcategory>
<subcategory>
<name>Subcategory 1.2</name>
<count>29</count>
</subcategory>
</subcategories>
</category>
<category>
<name>Category 2</name>
<subcategories>
<subcategory>
<name>Subcategory 2.1</name>
<count>18</count>
</subcategory>
<subcategory>
<name>Subcategory 2.2</name>
<count>29</count>
</subcategory>
</subcategories>
</category>
</theroot>
什麼是「配對」? – 2010-07-28 03:27:35