如何使用SimpleXML的XPath支持设置文本内容?

时间:2013-09-23 17:49:46

标签: php xml xpath simplexml

<xml>
    <ns:foo attribute="bar">
        OLD TEXT DATA
    </ns:foo>
</xml>

我使用XPath来处理XML命名空间。这不起作用:

$xml->asXML('old.xml');

foreach ($xml->xpath('ns:foo') as $foo) {
    $foo['attribute'] = 'new bar';
    $foo = 'NEW TEXT DATA'; //This won't be saved by asXML().
}

$xml->asXML('new.xml');

如何使用SimpleXMLElement::xpath更改文字内容?

1 个答案:

答案 0 :(得分:0)

$xml->asXML('old.xml');

foreach ($xml->xpath('ns:foo') as $foo) {
    $foo['attribute'] = 'new bar';
    $foo[0] = 'NEW TEXT DATA';
}

$xml->asXML('new.xml');