尝试这样的事情:
$objectX = "2"; // You get this value with $_POST or $_GET ...
$xmlFileName = 'my.xml'; // You're XML file
$xmlFile = file_get_contents($xmlFileName); // Saving the XML contents in a variable
$objects = new SimpleXMLElement($xmlFile);
$objectX = "object".$objectX; // The object name
$objects->$objectX->value++; // Incrementing the value
$objects->asXML($xmlFileName); // Saving the XML
echo $objects->$objectX->value; // echo the value
您必须添加<objects></objects>
到您的 XML 文件中:
<?xml version="1.0" encoding="UTF-8"?>
<objects>
<object1>
<value>10</value>
</object1>
<object2>
<value>6</value>
</object2>
</objects>