[英]Remove XML Child Node
我想知道是否有人可以幫助我。
我正在使用此頁面允許用戶查看其上傳圖像的畫廊。
上載后,圖像將保存在此文件路徑UploadedFiles/userid/locationid/image
, UploadedFiles/userid/locationid/image
的詳細信息(例如名稱,描述等)將保存在名為files.xml
的XML文件中,該文件與圖像位於同一目錄中。 摘錄如下:
<?xml version="1.0" encoding="utf-8" ?>
- <files>
<file name="AC-0003749-Clark_145520.jpg" source="AC-0003749-Clark_145520.jpg" size="3873" originalname="AC-0003749-Clark_145520.jpg" description="No description provided" userid="1" locationid="1" />
</files>
畫廊通過每個圖像下方的圖標為用戶提供了附加功能,可以刪除每個圖像。 這是通過以下代碼完成的:
圖標點擊事件
<script type="text/javascript">
Galleria.ready(function() {
this.$('thumblink').click();
$(".galleria-image").append(
"<span class='btn-delete ui-icon ui-icon-trash'></span>");
$(".btn-delete").live("click", function(){
var img = $(this).closest(".galleria-image").find("img");
// send the AJAX request
$.ajax({
url : 'delete.php',
type : 'post',
data : { image : img.attr('src') },
success : function(){
alert('Deleting image... ');
img.parent().fadeOut('slow');
}
});
return false;
});
});
</script>
delete.php-修改后的代碼
<?php
if (!empty($_POST)) {
$image = $_POST['image'];
if (file_exists($image)) {
unlink($image);
}
}
$doc = new DOMDocument;
$doc->load('files.xml');
$thedocument = $doc->documentElement;
$list = $thedocument->getElementsByTagName('files');
$nodeToRemove = null;
foreach ($list as $domElement){
if ($attrValue == '$image') { $domElement->parentNode->removeChild($domElement); }
}
if ($nodeToRemove != null)
$thedocument->removeChild($nodeToRemove);
echo $doc->saveXML();
?>
刪除物理圖像效果很好,但是在確定如何刪除相關子節點方面我遇到了很大的困難。 盡管我沒有收到錯誤消息,但該節點沒有被刪除。 我收到了一些指導, 在這里從這個網站了解如何去了解這一點,通過PHP的XML DOM IE,但說實話,我越了解這個,我就越感到困惑。 我似乎無法繞開它。
我只是想知道是否有人可以看看這個,並讓我知道我哪里出了問題。
非常感謝和問候
要刪除使用getElementsByTagName
找到的節點,可以使用以下命令將其刪除:
if (!empty($_POST)) {
$image = $_POST['image'];
if (file_exists($image)) {
unlink($image);
}
$doc = new DOMDocument;
$doc->load('files.xml');
// iterate over all tags named <file>
$list = $doc->getElementsByTagName('file');
foreach ($list as $domElement) {
// check whether attribute 'source' equals $image
if ($domElement->getAttribute('source') == $image) {
// remove the node
$domElement->parentNode->removeChild($domElement);
}
}
echo $doc->saveXML();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.