php使用DOMDocument删除xml节点

时间:2022-10-20 18:36:10

[update because of unclear question] thanks for pointing this out @hakre

[因为问题不明确而更新]感谢你指出这个@hakre

The problem I have is that with those script i have:

我遇到的问题是,我有这些脚本:

  1. or it cannot remove xml node i want
  2. 或者它无法删除我想要的xml节点
  3. or it removes the node, but it doesnt save it file
  4. 或者它删除节点,但它不保存它的文件

i have this xml document:

我有这个xml文档:

    <?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
    <Document>
        <Placemark>
            <name>Chelsea</name>
            <Snippet>London</Snippet>
            <Point>
                <coordinates>-0.2, 51.2,0.000000</coordinates>
            </Point>
            <id>1</id>
        </Placemark>
        <Placemark>
            <name>World</name>
            <Snippet>Willingdon </Snippet>
            <Point>
                <coordinates>-0.2,50.2,0.000000</coordinates>
            </Point>
            <id>2</id>
        </Placemark>
    </Document>
</kml>

and with this javascript i call delete.php:

并使用此javascript我调用delete.php:

function send2del(d){
            $.ajax({
                url : 'delete.php',
                type : 'POST',
                data : { what : $(d).data('id') },
                success : function(){
                       $(d).closest('tr').fadeOut('slow');
                }
            });
            return false;
        };

and i am trying to remove Placemark, using ID, here is my delete.php:

我试图删除地标,使用ID,这是我的delete.php:

<?php
if (isset($_POST['what'])) { 
    $id = $_POST['what']; 
    $doc = new DOMDocument; 
    $doc->load('placemarks.xml');

    $shops = $doc->getElementsByTagName('Placemark');
    foreach ($shops as $shop) {
        $ids = $shop->getElementsByTagName('id');
        if ($ids->item(0)->nodeValue == $id) {
            $shop->parentNode->removeChild($shop);
        } 
    }
    echo $doc->saveXML();
  }  
?>

But it does not work. I tried to us simpleXML, but with no success. Can you give me some guidelines about this problem? Or if you can help me with simpleXML solution, i would be very grateful.

但它不起作用。我尝试使用simpleXML,但没有成功。你能给我一些关于这个问题的指导吗?或者,如果您能帮助我使用simpleXML解决方案,我将非常感激。

1 个解决方案

#1


0  

I tested your code. It removes the element based on it. Maybe the issue resides with the ids used?

我测试了你的代码。它会根据它删除元素。也许问题在于使用的ID?

Before and After:

之前和之后:

BEFORE:

之前:

<kml xmlns="http://earth.google.com/kml/2.2">
<document>
<placemark>
<name>Chelsea</name>
<snippet>London</snippet>
<point>
<coordinates>-0.2, 51.2,0.000000</coordinates>
</point>
<id>1</id>
</placemark>
<placemark>
<name>World</name>
<snippet>Willingdon </snippet>
<point>
<id>2</id>
</placemark>
</document>
</kml>

AFTER:

后:

<kml xmlns="http://earth.google.com/kml/2.2">
<document>
<placemark>
<name>World</name>
<snippet>Willingdon </snippet>
<point>
<coordinates>-0.2,50.2,0.000000</coordinates>
</point>
<id>2</id>
</placemark>
</document>
</kml>

#1


0  

I tested your code. It removes the element based on it. Maybe the issue resides with the ids used?

我测试了你的代码。它会根据它删除元素。也许问题在于使用的ID?

Before and After:

之前和之后:

BEFORE:

之前:

<kml xmlns="http://earth.google.com/kml/2.2">
<document>
<placemark>
<name>Chelsea</name>
<snippet>London</snippet>
<point>
<coordinates>-0.2, 51.2,0.000000</coordinates>
</point>
<id>1</id>
</placemark>
<placemark>
<name>World</name>
<snippet>Willingdon </snippet>
<point>
<id>2</id>
</placemark>
</document>
</kml>

AFTER:

后:

<kml xmlns="http://earth.google.com/kml/2.2">
<document>
<placemark>
<name>World</name>
<snippet>Willingdon </snippet>
<point>
<coordinates>-0.2,50.2,0.000000</coordinates>
</point>
<id>2</id>
</placemark>
</document>
</kml>