PHP - 删除 XML 元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1153697/
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
PHP - Delete XML Element
提问by Ben Shelock
I need to delete elements of an XML file using PHP. It will be done via ajax and I need to find the XML element via an attribute.
我需要使用 PHP 删除 XML 文件的元素。它将通过 ajax 完成,我需要通过属性找到 XML 元素。
This is my XML file
这是我的 XML 文件
<?xml version="1.0" encoding="utf-8"?>
<messages>
<message time="1248083538">
<name>Ben</name>
<email>Ben's Email</email>
<msg>Bens message</msg>
</message>
<message time="1248083838">
<name>John Smith</name>
<email>[email protected]</email>
<msg>Can you do this for me?</msg>
</message>
</messages>
So what I would say is something like delete the element where the time equals 1248083838.
所以我想说的是删除时间等于 1248083838 的元素。
Ive been using Simple XML up until now and I've just realised it can do everything except delete elements.
到目前为止,我一直在使用 Simple XML,并且我刚刚意识到它可以执行除删除元素之外的所有操作。
So how would I do this?
那我该怎么做呢?
回答by Salty
Dave Morgan is correct in that DOM classes are more powerful, but in case you want to stick with SimpleXML, try using the unset() function on any node. E.g. unset($simpleXMLDoc->node1->child1), and that will be removed from the XML.
Dave Morgan 是正确的,因为 DOM 类更强大,但如果您想坚持使用 SimpleXML,请尝试在任何节点上使用 unset() 函数。例如 unset($simpleXMLDoc->node1->child1),这将从 XML 中删除。
回答by Dave Morgan
You can use the DOM classes in PHP. ( http://us3.php.net/manual/en/intro.dom.php).
您可以在 PHP 中使用 DOM 类。(http://us3.php.net/manual/en/intro.dom.php)。
You will need to read the XML document into memory, use the DOM classes to do manipulation, and then you can save out the XML as needed (to http or to file).
您需要将 XML 文档读入内存,使用 DOM 类进行操作,然后您可以根据需要将 XML 保存(到 http 或到文件)。
DOMNode is an object in there that has remove features (to address your question).
DOMNode 是一个具有删除功能的对象(以解决您的问题)。
It's a little more complicated than SimpleXML but once you get used to it, it's much more powerful
它比 SimpleXML 稍微复杂一点,但是一旦你习惯了它,它就会强大得多
(semi-taken from a code example at php.net)
(半取自 php.net 的代码示例)
<?php
$doc = new DOMDocument;
$doc->load('theFile.xml');
$thedocument = $doc->documentElement;
//this gives you a list of the messages
$list = $thedocument->getElementsByTagName('message');
//figure out which ones you want -- assign it to a variable (ie: $nodeToRemove )
$nodeToRemove = null;
foreach ($list as $domElement){
$attrValue = $domElement->getAttribute('time');
if ($attrValue == 'VALUEYOUCAREABOUT') {
$nodeToRemove = $domElement; //will only remember last one- but this is just an example :)
}
}
//Now remove it.
if ($nodeToRemove != null)
$thedocument->removeChild($nodeToRemove);
echo $doc->saveXML();
?>
This should give you a little bit of an idea on how to remove the element. It will print out the XML without that node. If you wanted to send it to file, just write the string to file.
这应该让您对如何删除元素有所了解。它将打印出没有该节点的 XML。如果要将其发送到文件,只需将字符串写入文件。
回答by Stefan Gehrig
回答by Ilari Kajaste
Even though SimpleXML doesn't have a detailed way to remove elements, you canremove elements from SimpleXML by using PHP's unset(). The key to doing this is managing to target the desired element. At least one way to do the targeting is using the order of the elements. First find out the order number of the element you want to remove (for example with a loop), then remove the element:
尽管 SimpleXML 没有删除元素的详细方法,但您可以使用 PHP 的unset(). 这样做的关键是设法瞄准所需的元素。至少一种定位方法是使用元素的顺序。首先找出要删除的元素的顺序号(例如使用循环),然后删除元素:
$target = false;
$i = 0;
foreach ($xml->message as $m) {
if ($m['time']=='1248083838') { $target = $i; break; }
$i++;
}
if ($target !== false) {
unset($xml->message[$target]);
}
You can even remove multiple elements with this, by storing the order number of target items in an array. Just remember to do the removal in a reverse order (array_reverse($targets)), because removing an item naturally reduces the order number of the items that come after it.
您甚至可以通过将目标项目的订单号存储在数组中来删除多个元素。请记住以相反的顺序 ( array_reverse($targets)) 进行删除,因为删除一个项目自然会减少它之后的项目的顺序号。
Admittedly, it's a bit of a hackaround, but it seems to work fine.
不可否认,这有点像hackaround,但它似乎工作正常。

