在php中编辑xml数据

时间:2014-04-15 08:37:00

标签: php xml

我希望此代码在“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>

1 个答案:

答案 0 :(得分:1)

好吧,$node->textContentreadonly值,因此您无法对其进行修改。

尝试使用$node->nodeValue代替:

$domElement = $elements->item(0);
$domElement->nodeValue = "newData";

$xml->save('myhappyfarmsavenew.xml');