dsgsdg206050 2013-05-12 23:45
浏览 44
已采纳

当用户使用PHP提交表单时,我需要找到一种编辑XML文件的方法

So I have a web page that displays the data of an XML file. However I have a form as well that should ideally be able to change the data. I figured the best way to accomplish this would be to use the Fopen function in PHP and edit the specific line of Text. The string won't be long, 4 words max. How would I use the fopen and the series of functions that open, read and write files to find a line of code and replace it?

  • 写回答

3条回答 默认 最新

  • doushuangai9733 2013-05-13 00:52
    关注

    I think you should do something like the following:

    1. Match the name of your input tags in your HTML to the nodes in your XML (this is only a suggestion to have your code more organized)

    2. When you post the data back to your server, in your Php code, using SimpleXML you can do as following

    Giving the following XML file

    <root>
        <config id="1">
            <name>old name</name>
            <category>old category</category>
        </config>
        <config id="2">
            <name>old name</name>
            <category>old category</category>
        </config>
    </root>
    

    //Load the XML file
    $xml = simplexml_load_file('PATH TO YOUR XML FILE');
    //Update the values that you want to update
    foreach($xml->root->config as $configGroup)
    {
        //Set the $IdToUpdate variable with the Id of the group that you want to update.
        if ($configGroup['id'] == $IdToUpdate)
        {
            $configGroup->name = $_POST['name' . $IdToUpdate];//Your html must have the proper input names.
            break;
        }
    }
    
    //Save the changes
    $xml->asXml('PATH TO YOUR XML FILE');
    

    Hope this helps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看