plus one to xml node value

This question already has an answer here:

  • A simple program to CRUD node and node values of xml file [closed] 2 answers

  • Try something like this:

    $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
    

    You have to add <objects></objects> to your XML file:

    <?xml version="1.0" encoding="UTF-8"?>
    <objects>
        <object1>
            <value>10</value>
        </object1>
        <object2>
            <value>6</value>
        </object2>
    </objects>
    
    链接地址: http://www.djcxy.com/p/29836.html

    上一篇: 将PHP中返回的XML数据保存为XML文件

    下一篇: 加一个到xml节点的值