tags:

views:

22

answers:

1

hi ! from this xml file i want to delete picture node according to attribute id. i have written php code but it does not work.

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

PHP code

$doc = new DOMDocument();
$doc->formatOutput = TRUE;
$doc->preserveWhiteSpace = FALSE;
$xPath = new DOMXPath($doc);
$doc->load('../Event_gallery.xml');
$query = sprintf('//pictures[./picture[@id="%s"]]', 38);
foreach ($xPath->query($query) as $node) {
    $node->parentNode->removeChild($node);
}
$doc->save('../Event_gallery.xml');

I think xpath is not working properly. control is not going in foreach

A: 

The problem is you are loading the document after you pass the document to DOMXPath.

Change

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

to

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

Then it should work.

To remove just the picture element with the given id instead of the whole parent, change the XPath to

//pictures/picture[@id="38"]
Gordon
one more thing i want to delete node picture only not pictures . This time it delete pictures. Can u help?
rajanikant
@rajanikant see update
Gordon