edit XML with simpleXML

Oliver Bayes-Shelton picture Oliver Bayes-Shelton · Jan 19, 2010 · Viewed 22.9k times · Source

How can I edit the value's in a xml file using simpleXML ?

I know how to create the file, but not how to edit the value in an existing file ?

Answer

cletus picture cletus · Jan 19, 2010

Sure you can edit with SimpleXML:

$input = <<<END
<?xml version='1.0' standalone='yes'?>
<documents>
  <document>
    <name>spec.doc</name>
  </document>
</documents>
END;

$xml = new SimpleXMLElement($input);
$xml->document[0]->name = 'spec.pdf';
$output = $xml->asXML();

Take a look at the examples.