鉴于PHP代码:
$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);
Run Code Online (Sandbox Code Playgroud)
我期望函数traverse()返回:
This is a link Title with some text following it.
Run Code Online (Sandbox Code Playgroud)
但是,它仅返回:
Title
Run Code Online (Sandbox Code Playgroud)
有没有办法使用simpleXML获得预期的结果(显然是为了消耗数据而不是像在这个简单的例子中那样返回它)?
谢谢,N.