Updating XML file using PHP

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

+2
xml php
Oct 05 '08 at 5:20
source share
1 answer

If you have PHP5 on your server, you can 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 Look at the spaces - XML ​​must be well-formed for SimpleXML for proper parsing.

Alternatives include simplexml_load_file() and simplexml_import_dom() .

+3
05 Oct '08 at 6:14
source share



All Articles