Skip to content

Instantly share code, notes, and snippets.

@bgadrian
Created March 16, 2015 17:34
Show Gist options
  • Save bgadrian/20c7cfcab68333a9f2f8 to your computer and use it in GitHub Desktop.
Save bgadrian/20c7cfcab68333a9f2f8 to your computer and use it in GitHub Desktop.
PHP simpleXML example modify
<?php
/* With simplexml, open an entire file (use XMLReader for big files)
Example : <root><s name="abc">value</s>....</root>*/
$xml = simplexml_load_file($xml_source_path);
//find the elemnt by attribute's value (name=..)
$result = $xml->xpath('//s[@name="'.$element_name.'"]');
//heres the trick - the first result of xpath, and the node value (stored in [0])
$result[0][0] = $new_value;
//here you can do a foreach, to cross over all results (all "s" elements with the same name"
if ($xml->asXML($xml_source_path) === false)
{
throw new Exception('Cannot save values into "'.$xml_source_path.'"',99);
}
return true;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment