Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 根据属性从XML中删除节点_Php_Xml - Fatal编程技术网

Php 根据属性从XML中删除节点

Php 根据属性从XML中删除节点,php,xml,Php,Xml,从这个xml文件中,我想根据属性id删除图片节点。 我已经编写了php代码,但它不起作用 <gallery> <organizate> <organization w="3" h="1" space="17"/> <organization w="4" h="2" space="17"/> <organization w="6" h="3" space="7"/>

从这个xml文件中,我想根据属性id删除图片节点。 我已经编写了php代码,但它不起作用

<gallery>
      <organizate>
        <organization w="3" h="1" space="17"/>
        <organization w="4" h="2" space="17"/>
        <organization w="6" h="3" space="7"/>
      </organizate>
      <pictures>
        <picture target="events/preview/10picture1.jpg" title="test1" movie="" text="test1" link="events_calender.php" id="38"/>
        <picture target="events/preview/8picture7.jpg" title="test2" movie="" text="cxvxc" link="events_calender.php" id="39"/>
        <picture target="events/preview/5picture10.jpg" title="test3" movie="" text="test3" link="events_calender.php" id="40"/>
      </pictures>
    </gallery>

我认为xpath工作不正常。控件没有进入foreach

问题是在将文档传递给DOMXPath后正在加载该文档

改变

$xPath = new DOMXPath($doc);
$doc->load('../Event_gallery.xml');

那么它应该会起作用

要仅删除具有给定id的picture元素而不是整个父元素,请将XPath更改为

//pictures/picture[@id="38"]

还有一件事我只想删除节点图片而不是图片。这次它会删除图片。你能帮忙吗?
$doc->load('../Event_gallery.xml');
$xPath = new DOMXPath($doc);
//pictures/picture[@id="38"]