php 和 simpleXml - 如何更改节点内容

发布于 2024-10-07 15:36:44 字数 1502 浏览 3 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木有鱼丸 2024-10-14 15:36:44

我敢说这段代码肯定不会做您想要的事情:

$newVerse = simplexml_load_string($strippedresponse);
$oldVerse = $xmlDoc->xpath("//div[@number='".$div1."']/div[@number='".$div2."']"); 
$oldVerse = $newVerse;

更改 PHP 变量的值没有副作用。换句话说,当您执行 < code>$a = $b; 除了某些特定情况外,它不是其中之一。

我不知道你真正想用这段代码实现什么目的。如果您想替换特定

内的 (X)HTML,您将需要使用 DOM 并创建一个 DOMDocumentFragment,使用 appendXML() 填充它将其替换为旧的

。或者创建一个新的 DOMDocumentloadXML(),然后 importNode() 到旧文档并replaceChild() > 你的旧分区。

I'll venture to say that this code certainly doesn't do what you want it to:

$newVerse = simplexml_load_string($strippedresponse);
$oldVerse = $xmlDoc->xpath("//div[@number='".$div1."']/div[@number='".$div2."']"); 
$oldVerse = $newVerse;

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 a new DOMDocument, loadXML() then importNode() to your old document and replaceChild() your old div.

疯到世界奔溃 2024-10-14 15:36:44

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文