php和simpleXml - 如何更改节点内容(php and simpleXml - how to change node contents)
我正在尝试使用simpleXML更改XML文件中节点的内容。 我知道新节点内容的变量包含正确的内容,但由于某种原因,文件在保存时不会更改。 我可能错过了一些基本的东西,因为我是simpleXML的新手。 这是整个PHP脚本:
<?php $doc=$_REQUEST["book"]; $div1=$_REQUEST["div1"]; $div2=$_REQUEST["div2"]; if ($div1=="") $div1=$_REQUEST["chapter"]; if ($div2=="") $div2=$_REQUEST["verse"]; $div3=$_REQUEST["div3"]; $textresponse=$_REQUEST["xmltext"]; $strippedresponse = "<?xml version='1.0'?>" . stripslashes($textresponse); echo("Saved changes to " . $doc . " " . $div1 . "." . $div2 ."<br />"); $fileName="/home/ocp/public_html/sites/default/docs/drafts/".$doc.".xml"; $xmlDoc = simplexml_load_file($fileName); $backupFileName="/home/ocp/public_html/sites/default/docs/backups/".$doc." ".date("Y-m-d H.i.s").".xml"; file_put_contents($backupFileName, $xmlDoc->asXML()); $backupSize = filesize($backupFileName); echo("Backup {$backupFileName} created:".$backupSize." bytes<br />"); if ($doc) { if ($div1) { if ($div2) { $newVerse = simplexml_load_string($strippedresponse); $oldVerse = $xmlDoc->xpath("//div[@number='".$div1."']/div[@number='".$div2."']"); $oldVerse = $newVerse; $newDoc = $xmlDoc->asXml(); file_put_contents($fileName, $newDoc); $newSize = filesize($fileName); echo("New file is ".$newSize." bytes <br />"); } } } ?>
I'm trying to change the contents of a node in an XML file using simpleXML. I know that the variable for the new node-contents contains the right stuff, but for some reason the file isn't changed when it is saved. I'm probably missing something basic, because I'm new to simpleXML. Here is the whole php script:
<?php $doc=$_REQUEST["book"]; $div1=$_REQUEST["div1"]; $div2=$_REQUEST["div2"]; if ($div1=="") $div1=$_REQUEST["chapter"]; if ($div2=="") $div2=$_REQUEST["verse"]; $div3=$_REQUEST["div3"]; $textresponse=$_REQUEST["xmltext"]; $strippedresponse = "<?xml version='1.0'?>" . stripslashes($textresponse); echo("Saved changes to " . $doc . " " . $div1 . "." . $div2 ."<br />"); $fileName="/home/ocp/public_html/sites/default/docs/drafts/".$doc.".xml"; $xmlDoc = simplexml_load_file($fileName); $backupFileName="/home/ocp/public_html/sites/default/docs/backups/".$doc." ".date("Y-m-d H.i.s").".xml"; file_put_contents($backupFileName, $xmlDoc->asXML()); $backupSize = filesize($backupFileName); echo("Backup {$backupFileName} created:".$backupSize." bytes<br />"); if ($doc) { if ($div1) { if ($div2) { $newVerse = simplexml_load_string($strippedresponse); $oldVerse = $xmlDoc->xpath("//div[@number='".$div1."']/div[@number='".$div2."']"); $oldVerse = $newVerse; $newDoc = $xmlDoc->asXml(); file_put_contents($fileName, $newDoc); $newSize = filesize($fileName); echo("New file is ".$newSize." bytes <br />"); } } } ?>
原文:https://stackoverflow.com/questions/4420499
更新时间:2021-11-17 17:11