我想知道是否有人可以喲幫我。使用jQuery刪除XML節點
我已經把this頁面,允許用戶查看自己上傳的圖片的畫廊。
在初始上傳,物理圖像被保存在以下文件結構: UploadedFiles/userid/locationid/image
和即描述等被保存在XML文件中的圖像的細節稱爲files.xml
這是在相同的目錄中的物理圖像。
我現在正在努力讓用戶能夠刪除這些圖像。
通過每幅圖像下刪除圖標的方式,我有,admitedly有一定的幫助,放在一起成功地刪除物理圖像以下。
'刪除圖標onclick事件'
<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);
}
}
?>
更新 '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){
$attrValue = $domElement->getAttribute('file_name');
if ($attrValue == 'image') {
$nodeToRemove = $domElement;
}
}
if ($nodeToRemove != null)
$thedocument->removeChild($nodeToRemove);
echo $doc->saveXML();
?>
我的問題正在從xml文件中刪除xml節點。我已經提供了下面的XML文件的摘錄。
<?xml version="1.0" encoding="utf-8" ?>
- <files>
<file name="stag.jpg" source="stag.jpg" size="21341" originalname="stag.jpg" description="No description provided" userid="1" locationid="1" />
</files>
我已經做了相當多的研究關於如何去了解這一點,並發現jQuery的有它自己的命令,即jQuery.remove
我本以爲這可以刪除節點。繼簡短的教程我增加了以下我的「onclick事件」腳本的末尾:
var doc = $(files.xml);
doc.find('file_name:src').remove();
不幸的是,雖然我沒有收到具體的錯誤,節點不被從文件中刪除。當談到XML時,我是一個完整的初學者,所以也許我看起來太簡單了。
我只是想知道wheteher有人也許可以看看這個,請讓我知道我要去哪裏錯了。
非常感謝和問候
嗨@Amogh Talpallikar,非常感謝您花時間回覆我的帖子。我對這種編程元素非常陌生,我只是想知道你是否可以精心製作這個節目,以便我知道我需要從哪裏開始。非常感謝和親切的問候 – IRHM
您將不得不使用delete.php從xml文件中刪除節點。 每當你刪除物理圖像時,你將不得不從XML中刪除它的節點。 –
簽出PHP XML DOM操作,使用PHP加載xml dom,然後從中刪除節點並將其保存回來。 –