由於PHP代碼獲取一個節點的文本部分:使用PHP簡單的XML
$xml = <<<EOF
<articles>
<article>
This is a link
<link>Title</link>
with some text following it.
</article>
</articles>
EOF;
function traverse($xml) {
$result = "";
foreach($xml->children() as $x) {
if ($x->count()) {
$result .= traverse($x);
}
else {
$result .= $x;
}
}
return $result;
}
$parser = new SimpleXMLElement($xml);
traverse($parser);
我預期的功能遍歷()返回:
This is a link Title with some text following it.
然而,它只返回:
Title
有沒有辦法使用simpleXML來獲得預期的結果(顯然是爲了消費數據而不是僅僅使用r就像在這個簡單的例子中一樣)?
感謝, N.
這實際上產生了我期待的確切結果 - 實際上並沒有解決遍歷Xml文檔的底層問題。 – Nikolaj 2011-02-27 16:28:35
您將無法使用SimpleXML「遍歷」或迭代文本節點。或者至少,如果可能的話,它將被設計/不切實際。使用SimpleXML讓東西變得更容易,然後將所選節點導入DOM,以訪問不屬於SimpleXML的功能。 – 2011-02-27 17:08:28
是的,確實如此。看來我必須學習那種困難的方式。 – Nikolaj 2011-02-27 17:17:11