使用jQuery删除XML节点 [英] Remove XML Node With jQuery

查看:286
本文介绍了使用jQuery删除XML节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人可以帮我.

I wonder whether someone may be able yo help me please.

我整理了页面,该页面允许用户查看画廊他们上传的图片.

I've put together this page which allows users to view a gallery of their uploaded images.

在初始上传时,物理图像将保存在以下文件结构中: UploadedFiles/userid/locationid/image和图像的详细信息(即描述等)保存在名为files.xml的xml文件中,该文件与物理图像位于同一目录中.

Upon initial upload, the physical images are saved in the following file structure: UploadedFiles/userid/locationid/image and the details of the image i.e. description etc are saved in an xml file called files.xml which is in the same directory as the physical images.

我正在努力允许用户删除这些图像.

I'm now working on allowing the user to be able to delete these images.

通过在每个图像下方都带有删除图标的方式,我诚然在一些帮助下,将以下内容组合在一起,成功删除了物理图像.

By way of a deletion icon under each image, I've, admitedly with some help, put together the following which successfully deletes the physical image.

删除图标Onclick事件"

'Deletion Icon Onclick Event'

<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'

Original '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文件的摘录.

The problem I'm having is deleting the xml node form the xml file. I've provided an extract of the XML file below.

  <?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事件"脚本的末尾添加了以下内容:

I've done quite a bit of research about how to go about this and found that jQuery had it's own command i.e. jQuery.remove which I thought would be able to delete the node. Following the brief tutorial I added the following to the end of my 'Onclick Event' script:

var doc = $(files.xml);
doc.find('file_name:src').remove();

不幸的是,尽管我没有收到特定的错误,但并未从文件中删除该节点.在XML方面,我是一个完整的初学者,所以也许我过于单调地看待这个问题.

Unfortunately, although I don't receive a specific error, the node isn't being deleted from the file. I'm a complete beginner when it comes to XML so perhaps I'm looking at this too simplistically.

我只是想知道是否有人可以看看这个,让我知道我要去哪里了.

I just wondered wheteher someone could perhaps have a look at this please and let me know where I'm going wrong.

非常感谢和问候

推荐答案

这是因为JavaScript(JQuery)将XML DOM加载到内存中,然后在删除节点时, 它会从内存中的xml文档(对象)中删除.

This is because JavaScript(JQuery) loads the XML DOM in memory and then when you delete a node, it gets deleted from the in-memory xml doc(the object).

它不会从物理XML文件中删除.

It wont be removed from the physical XML file.

JS在沙盒浏览器环境中运行,并且无法更改系统上的本地文件. 如果您尝试从远程服务器加载xml,那么这是一个非常糟糕的主意.

JS runs in a sandbox Browser environment and cannot alter local files on the system. and if you are trying to load xml from a remote server then its a very bad idea.

从远程服务器下载XML文件作为临时文件,然后当您再次加载XML时,将创建一个内存中的DOM,并从中删除该节点.

the XML file from remote server is downloaded as temp file and then when you load XML again an in-memory DOM is created and the node is deleted from it.

因此,如果您希望更改实际文件, 您将需要使用AJAX并将一些HTTP请求发送到您的服务器,以对物理文件执行相同的操作.

So in case you want the actual file to be changed, you will need to use AJAX and send some HTTP request to your server to do the same to the physical file.

更新:

查看本教程 http://www.phpeveryday.com/articles/PHP-XML -Removing-Node-P415.html

并尝试将XML文件加载到delete.php中,并从中删除相应的节点,然后将该xml保存回将被覆盖的原始文件中.

and try to load the xml file in your delete.php and remove the corresponding node from it and then save this xml back to the original file which will be overwritten.

这篇关于使用jQuery删除XML节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆