php 删除 simplexmlelement 节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9643116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:20:41  来源:igfitidea点击:

Deleting simplexmlelement node

phpxmlsimplexml

提问by user823527

I have an xml file of this structure

我有一个这种结构的xml文件

<?xml version="1.0" encoding="iso-8859-1"?>
<my_events>
                        <event id="e20111129215359">
                            <title>the title</title>
                            <channel id="1">
                                <name>A name</name>
                                <onclick></onclick>
                            </channel>
                            <event_site>
                                <name/>
                                <url/>
                            </event_site>
                            <start_date>Thu Mar 08 2012</start_date>
                            <start_time>11:00 AM</start_time>
                            <end_date>null</end_date>
                            <end_time>null</end_time>
                            <notes>Notes for the event</notes>
                        </event>
 </my_events>

To delete an event, I have this php function.

要删除一个事件,我有这个 php 函数。

<?php

include_once("phpshared.php");

function delete_event( $nodeid ) {

    $nodes = new SimpleXMLElement('my_events.xml', LIBXML_NOCDATA, true);

    $node = $nodes->xpath("/my_events/event[@id='$nodeid']");

    $node->parentNode->removeChild($node);

    $formatted = formatXmlString($nodes->asXML());
    $file = fopen ('my_events.xml', "w"); 
    fwrite($file, $formatted); 
    fclose ($file);  

}

echo delete_event(trim($_REQUEST['nodeid']));

?>

That doesn't delete the node. Is there a different way to do this?

这不会删除节点。有没有不同的方法来做到这一点?

回答by salathe

SimpleXML allows removal of elements via PHP's unset()keyword.

SimpleXML 允许通过 PHP 的unset()关键字删除元素。

For your code snippet, simply replace

对于您的代码片段,只需替换

$node->parentNode->removeChild($node);

with

if ( ! empty($node)) {
    unset($node[0][0]);
}

If the XPath query returned a matching <event>element, we instruct SimpleXML to unset()it.

如果 XPath 查询返回匹配的<event>元素,我们unset()会将SimpleXML 指示给它。



Aside:here are two occurrences of [0]because:

旁白:这里有两种情况,[0]因为:

  • xpath()returns an array, even if only one element matches. So [0]is used to get the first item in that array, which is the element we want to delete.
  • The SimpleXMLElement returned from $node[0]represents a collection of <event>elements (but if you access elements/attributes on it then the values from the first in the collection is used). So, we use [0]to get at the actual SimpleXMLElementthat we want to delete, which is the first in this magical collection.
  • xpath()返回一个数组,即使只有一个元素匹配。So[0]用于获取该数组中的第一项,即我们要删除的元素。
  • 返回的 SimpleXMLElement$node[0]表示<event>元素集合(但如果您访问其上的元素/属性,则使用集合中第一个的值)。所以,我们使用[0]来获取SimpleXMLElement我们想要删除的实际值,这是这个神奇集合中的第一个。