Voting

: three minus one?
(Example: nine)

The Note You're Voting On

Nibinaear
17 years ago
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);

?>

<< Back to user notes page

To Top