PHP 및 SimpleXML을 사용하여 XML 파일에서 고객의 값을 편집하려고합니다. 파일에 새 데이터를 쓰고 고객의 값을 편집 할 수있는 것처럼 보일 수 있지만 변경 사항을 파일에 저장할 수는 없습니다.PHP + SimpleXML로 업데이트 된 XML 파일을 저장할 수 없습니다.
이 프로젝트가 내 서버에 있지만 변경 사항이없는 chmod 777 -R을 시도했으며 동일한 데이터를 다른 방법으로 동일한 customers.xml 파일에 쓸 수 있으므로 문제가 제외됩니다. 권한이 있습니다. 또한, 좋은 결과가 file_put_contents() 함께 파일을 저장하는 시도했다.
이 함수의 의도 된 목표는 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);
}
위해 var_dump()의 출력, 호머에 고객 바트의 이름을 변경 한 후 :
<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>
어떤 도움 것 :
또한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"
}
}
}
, 여기 내 XML 문서의 구조는 크게 감사드립니다! 편집 된 데이터가 파일에 올바르게 저장되는 것을 좋아합니다!