Replace the value of a SimpleXMLElement element

It may be simple, but I could not find a single example on the Internet. I need to find node using xpath and replace it.

This is a small version of an xml document:

<?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> 

And this is my php code:

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

Basically, John Doe should be George Doe

+4
source share
1 answer

I have found a solution. Basically, since the xpath function returns SimpleXMLElement Object I need to access it:

 // the following code doesn't work... $xml->$result = "George Dow"; // but this does :D $result[0][0] = "George Dow"; 
+4
source

All Articles