我希望此代码在“platform”标记中替换为“newData”
$xml = new DOMDocument();
$xml->load('myhappyfarmsave.xml');
$xpath = new DOMXPath($xml);
$elements = $xpath->query('/myhappyfarmsave/platform');
$domElement = $elements->item(0);
var_dump($domElement->textContent);
$domElement->textContent = "newData";
$xml->save('myhappyfarmsavenew.xml');
我不知道我的代码有什么问题。谢谢你的帮助
<?xml version="1.0" encoding="UTF-8"?>
<myhappyfarmsave version="3">
<platform>Bazaar</platform>
<timestamp>1356876534</timestamp>
<lastlogtime>1356823542</lastlogtime>
</myhappyfarmsave>
答案 0 :(得分:1)
好吧,$node->textContent
是readonly
值,因此您无法对其进行修改。
尝试使用$node->nodeValue
代替:
$domElement = $elements->item(0);
$domElement->nodeValue = "newData";
$xml->save('myhappyfarmsavenew.xml');