PHP DOM删除子对象
我试图从xml中删除PHP DOM删除子对象,php,xml,dom,simplexml,Php,Xml,Dom,Simplexml,我试图从xml中删除的父节点,如果它的内容符合某个标准,但它一直只删除一个节点。如何删除整个父节点 这是我的密码: $xml = new SimpleXMLElement('<xml/>'); if (file_exists("xml/units/E01.xml")) { $xml = simplexml_load_file("xml/units/E01.xml"); echo "File exists"; echo "</br>";
的父节点,如果它的内容符合某个标准,但它一直只删除一个节点
。如何删除整个父节点
这是我的密码:
$xml = new SimpleXMLElement('<xml/>');
if (file_exists("xml/units/E01.xml")) {
$xml = simplexml_load_file("xml/units/E01.xml");
echo "File exists";
echo "</br>";
$wcccanumber = "121202482";
foreach ($xml->call->wcccanumber as $call) {
if ($call == $wcccanumber) {
$dom = dom_import_simplexml($call);
$dom->parentNode->removeChild($dom);
$fp = fopen("xml/units/E01.xml","wb");
fwrite($fp,$xml->asXML());
fclose($fp);
}
}
}
$xml=新的SimpleXMLElement(“”);
如果(文件_存在(“xml/units/E01.xml”)){
$xml=simplexml_load_文件(“xml/units/E01.xml”);
echo“文件存在”;
回声“”;
$wcccanumber=“121202482”;
foreach($xml->call->wcccanumber作为$call){
如果($call==$wcccanumber){
$dom=dom\u import\u simplexml($call);
$dom->parentNode->removeChild($dom);
$fp=fopen(“xml/units/E01.xml”,“wb”);
fwrite($fp,$xml->asXML());
fclose($fp);
}
}
}
以下是xml:
<xml>
<call>
<wcccanumber>121202482</wcccanumber>
<currentcall>FALL</currentcall>
<county>W</county>
<id>82</id>
<location>234 E MAIN ST</location>
<callcreated>12:26:09</callcreated>
<station>HBM</station>
<units>E01</units>
<calltype>M</calltype>
<lat>45.5225067888299</lat>
<lng>-122.987112718574</lng>
<inputtime>12/18/2012 12:27:01 pm</inputtime>
</call>
</xml>
121202482
落下
W
82
东大街234号
12:26:09
HBM
E01
M
45.5225067888299
-122.987112718574
2012年12月18日12:27:01下午
遍历call
并将$call->wcccanumber
与$wcccanumber
进行比较。将$call
转换为dom并删除它(parentNode->removeChild
)
如果存在多个删除,则在完成所有删除后只保存一次是有意义的
$deletionCount = 0;
foreach ($xml->call as $call) {
if ($call->wcccanumber != $wcccanumber) {
continue;
}
$dom = dom_import_simplexml($call);
$dom->parentNode->removeChild($dom);
$deletionCount++;
}
if ($deletionCount) {
file_put_contents("xml/units/E01.xml", $xml->asXML());
}
你想删除call的所有子节点,对吗?是的,我会删除,然后我会修改更多代码来替换,但我还不知道如何删除它们。我最终想删除整个节点,包括callpsst:谢谢。效果是原来的10倍。谢谢我不知道我自己怎么想不出来…有时看不到森林中的树:)好吧,实际上,如果有一个或多个删除,你应该在
foreach
之后保存,这样你总是在所有更改后保存。因此,这会删除“”括号,但保留所以当我插入一个新的孩子时,它会把我的代码搞乱!
$deletionCount = 0;
foreach ($xml->call as $call) {
if ($call->wcccanumber != $wcccanumber) {
continue;
}
$dom = dom_import_simplexml($call);
$dom->parentNode->removeChild($dom);
$deletionCount++;
}
if ($deletionCount) {
file_put_contents("xml/units/E01.xml", $xml->asXML());
}