简体   繁体   English

无法保存已用PHP + SimpleXML更新的XML文件

[英]Can't save XML file that's been updated with PHP + SimpleXML

I'm trying to edit the values of a customer in an XML file using PHP and SimpleXML. 我正在尝试使用PHP和SimpleXML在XML文件中编辑客户的值。 I can seem to be able to write new data to the file, and edit the values of a customer but I cannot get those changes to save to the file. 我似乎能够将新数据写入文件,并编辑客户的值,但是我无法将这些更改保存到文件中。

I have tried to chmod 777 -R where this project is located on my server but nothing changes, and I can also write new data to the same customers.xml file in another method, so I'm ruling out an issue with permissions. 我尝试使用chmod 777 -R将该项目放置在服务器上,但没有任何更改,并且我还可以使用另一种方法将新数据写入相同的customer.xml文件,因此我排除了权限问题。 Additionally, I have tried saving the file with file_put_contents() with no good results. 此外,我尝试使用file_put_contents()保存文件,但效果不佳。

The intended goal of this function is to do an XPath expression to get the customer that is to be modified, update the values of that customer, then save the edited changes of that customer to file, as well the rest of the XML document - which should be unchanged, since only one customer can be updated at a time. 此功能的预期目标是执行XPath表达式以获取要修改的客户,更新该客户的值,然后将该客户的编辑后的更改以及XML文档的其余部分保存到文件中-应该保持不变,因为一次只能更新一个客户。

$customersXml = simplexml_load_file('customers.xml', null, true) or die('Error: Cannot load XML from file customers.xml"');
$customerToEdit = $customersXml->xpath('//customer[position()="'.$customerIndexOfMatch.'"]/customerInfo');

if (isset($_POST['updateCustomer'])){
    // updating the data seems to work fine
    $customerToEdit[0]->firstName = $_POST['edit-fname'];
    $customerToEdit[0]->middleName = $_POST['edit-mname'];
    $customerToEdit[0]->lastName = $_POST['edit-lname'];
    $customerToEdit[0]->address = $_POST['edit-address'];
    $customerToEdit[0]->telephone[0] = $_POST['edit-ph1'];
    $customerToEdit[0]->telephone[1] = $_POST['edit-ph2'];
    $customerToEdit[0]->telephone[2] = $_POST['edit-ph3'];

    // but when it comes to saving the updated data, i'm baffled (this method of saving has worked in another function)
    // have also tried file_put_contents('customers.xml', $customersXml->asXML()) which doesn't work here
    $customersXml->asXML('customers.xml');

    echo '<pre>';
    var_dump($customerToEdit);                          
}

The output of var_dump(), after changing customer Bart's name to Homer: 将客户Bart的名称更改为Homer后,var_dump()的输出:

array(1) {
  [0]=>
  object(stdClass)#3 (5) {
    ["firstName"]=>
    string(4) "Homer"
    ["middleName"]=>
    string(0) ""
    ["lastName"]=>
    string(7) "Simpson"
    ["address"]=>
    string(28) " ... "
    ["telephone"]=>
    array(3) {
      [0]=>
      string(10) "111222333"
      [1]=>
      string(10) "444555666"
      [2]=>
      string(10) "777888999"
    }
  }
}

Also, here is the structure of my XML document: 另外,这是我的XML文档的结构:

<customers>
    <customer number="" meterNumber=""> 
        <customerInfo>
            <title></title> 
            <firstName></firstName>
            <middleName></middleName>
            <lastName></lastName>
            <address></address>
            <telephone></telephone>
            <telephone></telephone>
            <telephone></telephone>
        </customerInfo>

        <prevMeterReadings>
            <reading />
        <prevMeterReadings>
    </customer>
</customers>

Any help would be greatly appreciated! 任何帮助将不胜感激! Would love for the edited data to save correctly to file! 希望将编辑后的数据正确保存到文件中!

Ok so I've figured out what the issue was. 好的,我知道了问题所在。

The solution was to change these lines from: 解决方案是将这些行更改为:

$customerToEdit[0]->firstName = $_POST['edit-fname'];
...

to

$customersXml->customer[$customerIndexOfMatch]->customerInfo->firstName = $_POST['edit-fname'];
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM