note 81541 deleted from function.dom-domxpath-query by nlopess
| From: | nlopess@php.net | Date: | Mon, 03 Mar 2008 15:55:45 +0000 |
| Subject: | note 81541 deleted from function.dom-domxpath-query by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-136697@lists.php.net to get a copy of this message | ||
Note Submitter: Nibinaear
----
I've searched the entire web looking for a way to update / modify/ change/ alter the elements
of an xml file and found NOTHING!
So here it is, the defninitive way to "Change XML elements with PHP" rather than adding /
appending new ones. This uses XPATH:
<?php
// Create a DOMDocument instance
$xml = new DOMDocument;
// Ignore whitespace between nodes (default: true)
$xml->preserveWhiteSpace = false;
$file='about.xml';
// Load the XML data source
$xml->Load($file);
$xpath = new DOMXPath($xml);
$query='/regions/branch';
$entries = $xpath->query($query);
foreach ($entries as $entry)
{
$entry->firstChild->nodeValue="like this!";
echo $entry->firstChild->nodeValue;
}
$xml->save($file);
?>