我有一個SimpleXML的元素,從XML類似如下:遍歷的SimpleXML元素通過通配符查詢
<Result>
<BlueAnswer>Blue</BlueAnswer>
<RedAnswer>Red</RedAnswer>
<YellowAnswer>Yellow</YellowAnswer>
<BlueQuestion>What is my favorite color?</BlueQuestion>
<RedQuestion>What is my favorite color?</RedQuestion>
<YellowQuestion>What is my favorite color?</YellowQuestion>
</Result>
我如何遍歷他們一個通配符,像這樣:
foreach ($response->Result->wildcard('*Answer') as $answer) {
echo "Answer: " . $answer;
}
這似乎很簡單,但我似乎無法弄清楚。感謝您提前提供任何幫助。
你可能正在尋找的是[Xpath](http://www.php.net/manual/en/simplexmlelement.xpath.php) - 但是你不能只是循環'$ response-> Result'? – DaveRandom 2012-01-16 18:27:52
它比這更復雜,因爲'$ response-> Result'中還有其他子元素。我已經更新了我的問題。 – element119 2012-01-16 18:30:12
[節點名稱上的XPath通配符]的可能重複(http://stackoverflow.com/questions/4203119/xpath-wildcards-on-node-name) – Wrikken 2012-01-16 18:44:47