Php 如何根据特定XML子节点的值删除它们
我的XML结构如下所示:Php 如何根据特定XML子节点的值删除它们,php,javascript,xml,Php,Javascript,Xml,我的XML结构如下所示: <document> <sliderimage> <images>click.bmp</images> <images>error_inotherpluginupload.JPG</images> <images>dddd.jpg</images> <images>Sunset123.jpg&
<document>
<sliderimage>
<images>click.bmp</images>
<images>error_inotherpluginupload.JPG</images>
<images>dddd.jpg</images>
<images>Sunset123.jpg</images>
<images>Water lilies.jpg</images>
</sliderimage>
</document>
单击.bmp
其他插件中的错误\u pluginupload.JPG
dddd.jpg
Sunset123.jpg
睡莲
我展示的这些图像如下:
<document>
<sliderimage>
<images>click.bmp</images>
<images>error_inotherpluginupload.JPG</images>
<images>dddd.jpg</images>
<images>Sunset123.jpg</images>
<images>Water lilies.jpg</images>
</sliderimage>
</document>
获取图像的代码,您可以在此处看到:
<?php
$usernmeforxml = $_SESSION['username'];
$xmlpath = SITE_URL . "xml/" . $usernmeforxml . "/test.xml";
$xml = simplexml_load_string(file_get_contents($xmlpath));
$sliderimagesinner = $xml->sliderimage->images;
$imagenum = count($sliderimagesinner);
?>
</br>
<?php
for ($i = 0; $i < $imagenum; $i++)
{
?>
<a class="thumbnail" href="#thumb">
<img src="../slider_images/<?php
echo $sliderimagesinner[$i];
?>" width="120" height="70" />
<span>
<img src="../slider_images/<?php
echo $sliderimagesinner[$i];
?>" width="500" height="350" />
</span>
</a>
<?php
}
?>
现在我想通过在图像上添加“删除”按钮来删除图像。单击时,应将该图像节点从上述XML文件中删除
所有的事情都是动态的,所以如果有人能在这方面帮助我,那将是非常有帮助的
$usernmeforxml=$_SESSION['username'];
$xmlpath=SITE_URL."xml/".$usernmeforxml."/test.xml";
$xml = simplexml_load_file($xmlpath);
if($_GET['remove'] == 'true') {
$myValue = $_GET['file'];
$xel_array = $xml->xpath("//images[text()='" . $myValue . "']");
//edited - forgot to double [0]
unset($xel_array[0][0]);
file_put_contents($xmlpath, $xml->asXML());
}
和一个像
<a href="_your_page.php?remove=true&file=your_file_name">remove</a>
应该这样做。这不是一个真正的答案,因为我不知道如何解决这个问题,但如果我遇到这个问题,我会开始在PHP的DOM类中寻找解决方案,其中包括一个DOMXQuery类。使用XQuery应该可以找到满足非常特定标准的节点。是否可以更改结构以为每个图像添加id属性?这将使删除操作不那么模棱两可。@GordonM没有
DOMXQuery
,但是DOMXPath
应该足够了。@fab你说得对,我指的是XPath。把这一点归结为缺少早餐咖啡:)就像我在functions.php上使用我的查询一样,因此在a href中,我必须添加一些类似的内容,您可以将代码添加到当前文件的顶部,并用slider.php替换您的_page.php或任何您的文件名,用xml中的名称替换您的_文件名。。。例如:睡莲。由于您的文件可能包含需要转义的字符,您可能希望使用urlencode和URLEDECODE。