php 和 simpleXml - 如何更改节点内容
我正在尝试使用 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 />");
}
}
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我敢说这段代码肯定不会做您想要的事情:
更改 PHP 变量的值没有副作用。换句话说,当您执行 < code>$a = $b; 除了某些特定情况外,它不是其中之一。
我不知道你真正想用这段代码实现什么目的。如果您想替换特定
内的 (X)HTML,您将需要使用 DOM 并创建一个 DOMDocumentFragment,使用 appendXML() 填充它将其替换为旧的
。或者创建一个
新的 DOMDocument
、loadXML()
,然后importNode()
到旧文档并replaceChild()
> 你的旧分区。I'll venture to say that this code certainly doesn't do what you want it to:
Changing the value of a PHP variable has no side-effects. In other word, nothing happens when you do
$a = $b;
except in some specific cases, and it's not one of them.I don't know what you really want to achieve with this code. If you want to replace the (X)HTML inside a specific
<div/>
you will need to use DOM and create a DOMDocumentFragment, use appendXML() to populate it then substitute it to your old<div/>
. Either that or create anew DOMDocument
,loadXML()
thenimportNode()
to your old document andreplaceChild()
your old div.SimpleXMLElement::xpath
返回 SimpleXMLElement 对象的数组。副本,而非参考文献。因此$oldVerse = $newVerse;
不会以任何方式更改$xmlDoc
。SimpleXML 足以读取 XML,对于操作,您可能需要从 中选择更强大的替代方案http://www.php.net/manual/de/refs.xml.php,例如 DOM。
SimpleXMLElement::xpath
returns an array of SimpleXMLElement objects. Copies, not references. So$oldVerse = $newVerse;
does not change$xmlDoc
in any way.SimpleXML is sufficient to read XML, for manipulation you might want to choose a more powerful alternative from http://www.php.net/manual/de/refs.xml.php, e.g. DOM.