Created
March 16, 2015 17:34
-
-
Save bgadrian/20c7cfcab68333a9f2f8 to your computer and use it in GitHub Desktop.
PHP simpleXML example modify
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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