This may be simple but I couldn't find any example on the web. I need to find a node using xpath and replace it's value.

This is a small version of the xml document:

<?xml version="1.0" encoding="utf-16" standalone="yes"?>
<w:document xmlns:w="">
            <w:t>John Doe</w:t>

And this is my php code:


$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();


Basically, John Doe should be George Dow

Accepted Answer

I found the solution. Basically, since the xpath function returns a SimpleXMLElement Object an array, I need to access it as one:

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

// but this does :D
$result[0][0] = "George Dow";
Written by wilsonsilva
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the community.
It is licensed under cc-wiki