Recently, you need to use XML to perform operations on some XML files. Refer to the simplexml Operation Method in the PHP manual:
The XML file is as follows:
<?xml version="1.0" encoding="iso-8859-1"?><Template> <PhotoObjects> <PhotoObject> <id>1</id> <name>null</name> <x>0.0257937</x> <y>0.3936667</y> <w>0.2783334</w>
After the following processing (some variable values are sent through the $ _ Get [] request ):
$ Xml = simplexml_load_file ('/example. xml ');
foreach($xml->PhotoObjects->PhotoObject as $po){if ($po->id == $pId) {//add panx ,pany, zoom $po->addChild('panx', $panx);$po->addChild('pany', $pany);$po->addChild('zoom', $zoom);}}$newXML = $xml->asXML();$fp = fopen('/example.xml', "w");fwrite($fp, $newXML);fclose($fp);
<?xml version="1.0" encoding="iso-8859-1"?><Template> <PhotoObjects> <PhotoObject> <id>1</id> <name>null</name> <x>0.0257937</x> <y>0.3936667</y> <w>0.2783334</w>
Because we need to re-write the XML file back to the corresponding file, we need to use the asxml () method.