tags:

views:

1651

answers:

1

What is the easiest way to update a single attribute in a XML tag using PHP without rewriting and saving the file? Any way to do this just using regular DOM stuff?

+3  A: 

If you have PHP5 on your server you could try:

$string = "<?xml version='1.0'?>
<doc>
 <title>XML Document</title>
 <date timezone=\"GMT+1\">2008-01-01 13:42:53</date>
 <message>Daylight savings starting soon!</message>
</doc>";

$xml = simplexml_load_string($string);

// Show current timezone
echo $xml->date['timezone'].'<br>';

// Set a new timezone
$xml->date['timezone'] = 'GMT+10';
echo $xml->date['timezone'];

Note: Watch the whitespace -- the XML needs to be well-formed for SimpleXML to parse correctly.

Alternatives include simplexml_load_file() and simplexml_import_dom().

mattoc