2

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?

1 Answer 1

3

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().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.