Updating XML node with PHP

You’re not accessing the right node. In your example, $xml holds the root node <info/>. Here’s a great tip: always name the variable that holds your XML document after its root node, it will prevent such confusion.

Also, as Ward Muylaert pointed out, you need to save the file.

Here’s the corrected example:

// load the document
// the root node is <info/> so we load it into $info
$info = simplexml_load_file('test.xml');

// update
$info->user->name->nameCoordinate->xName = $xPostName;
$info->user->name->nameCoordinate->yName = $yPostName;

// save the updated document
$info->asXML('test.xml');

Leave a Comment