【发布时间】:2011-07-05 04:06:26
【问题描述】:
给定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);
我希望函数 traverse() 返回:
This is a link Title with some text following it.
但是,它只返回:
Title
有没有办法使用 simpleXML 获得预期的结果(显然是为了消费数据,而不是像这个简单的例子那样仅仅返回它)?
【问题讨论】: