Php 替换SimpleXMLElement的值

Php 替换SimpleXMLElement的值,php,xml,simplexml,Php,Xml,Simplexml,这可能很简单,但我在网上找不到任何例子。我需要使用xpath找到一个节点并替换它的值 这是xml文档的一个小版本: <?xml version="1.0" encoding="utf-16" standalone="yes"?> <w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> <w:body> </w:p>

这可能很简单,但我在网上找不到任何例子。我需要使用xpath找到一个节点并替换它的值

这是xml文档的一个小版本:

<?xml version="1.0" encoding="utf-16" standalone="yes"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:body>
    </w:p>
        <w:r>
            <w:t>John Doe</w:t>
        </w:r>
    </w:p>
  </w:body>
</w:document>

无名氏
这是我的php代码:

<?php

$xml = simplexml_load_file("doc1/word/document.xml");
$result = $xml->xpath("/w:document/w:body/w:p[1]/w:r[1]/w:t[1]");

// the following code doesn't work...
$xml->$result = "George Dow";

echo $xml->asXML();

?>
xpath(“/w:document/w:body/w:p[1]/w:r[1]/w:t[1]”);
//以下代码不起作用。。。
$xml->$result=“George Dow”;
echo$xml->asXML();
?>

基本上,John Doe应该是George Dow,我找到了解决方案。基本上,由于xpath函数以数组形式返回SimpleXMLElement对象,因此我需要以数组形式访问它:

// the following code doesn't work...
$xml->$result = "George Dow";

// but this does :D
$result[0][0] = "George Dow";