小编Nik*_*laj的帖子

使用php Simple XML获取节点的文本部分

鉴于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.

php simplexml

7
推荐指数
2
解决办法
2万
查看次数

标签 统计

php ×1

simplexml ×1