tags:

views:

31

answers:

2

hi every one , in my xml file i want to remove record element according to title My xml file is

<?xml version="1.0"?>
<gallerylist>
  <record>
    <movie>videos/Avatar_HD.flv</movie>
    <title>Title:</title>
    <desc>Description</desc>
    <preview>videos/previews/avatar.jpg</preview>
    <imgplaylist>videos/imgplaylist/p1.jpg</imgplaylist>
    <category>Category</category>
  </record>
 <record>
    <movie>videos/The_Princess_And_The_Frog_HD.flv</movie>
    <title></title>
    <desc>fdgdd</desc>
    <preview>videos/previews/frog.jpg</preview>
    <imgplaylist>videos/imgplaylist/p4.jpg</imgplaylist>
    <category>Category1</category>
 </record>
    <record>
        <movie>videos/Prince_of_Persia_The_Sands_of_Time_HD.flv</movie>
        <title>Title:2</title>
        <desc>xzcXZ</desc>
        <preview>videos/previews/sandsoftime.jpg</preview>
        <imgplaylist>videos/imgplaylist/p2.jpg</imgplaylist>
        <category>Category2</category>
    </record>
    <record>
        <movie>videos/Sherlock_Holmes_HD.flv</movie>
        <title>Title:4</title>
        <desc>dfgdf</desc>
        <preview>videos/previews/sherlock.jpg</preview>
        <imgplaylist>videos/imgplaylist/p7.jpg</imgplaylist>
        <category>Category4</category>
    </record>
</gallerylist>

and my php file is

        <?php

          $doc = new DOMDocument; 
           $doc->load('playlist.xml');

            $thedocument = $doc->documentElement;

             $list = $thedocument->getElementsByTagName('title');
           $nodeToRemove = null;
              foreach ($list as $domElement){
                  $attrValue = $domElement->nodeValue;
                 if ($attrValue == 'Title:4') {
                  $nodeToRemove = $domElement; 
                         }
                      }


             if ($nodeToRemove != null)
               $thedocument->removeChild($nodeToRemove);

                  $doc->saveXML(); 
                      ?>

it gives following error:-

Fatal error: Uncaught exception 'DOMException' with message 'Not Found Error' in D:\wamp\www\funkeymusic\admin\update_video.php:22 Stack trace: #0 D:\wamp\www\funkeymusic\admin\update_video.php(22): DOMNode->removeChild(Object(DOMElement)) #1 {main} thrown in D:\wamp\www\funkeymusic\admin\update_video.php on line 22

+2  A: 

You can only call removeChild() on the respective parent node. Since the $nodeToRemove is not a direct child of $thedocument (it is a descendant), you get the "not found" error.

if ($nodeToRemove != null) {
  $nodeToRemove->parentNode->removeChild($nodeToRemove);
}
Tomalak
but it is not removing from file
Rajanikant
@Rajanikant: What does it do instead?
Tomalak
it shows blank page or whet i printing
Rajanikant
+1  A: 

From the question I understood you want to remove the <record> elements with a <title> child that contains a specific text. The solution is more verbose than it needs to be and $attrValue is suggesting the DOMText of the title element is an Attribute, which it isnt. But anyway, let's remove all this and use XPath:

$searchString = 'Title:4';

$doc = new DOMDocument;
$doc->formatOutput = TRUE;
$doc->preserveWhiteSpace = FALSE;
$doc->load('playlist.xml');

$xPath = new DOMXPath($doc);
$query = sprintf('//record[./title[contains(., "%s")]]', $searchString);
foreach($xPath->query() as $node) {
    $node->parentNode->removeChild($node);
}

echo $doc->saveXML();

The XPath says, find all record nodes, which have a child title with a text node containing the search string. Note that containing, does not mean is equal to, so if you would use "Title:" as $searchString, it would remove all movies but "The_Princess_And_The_Frog_HD". If you want to remove exact titles, change the XPath to

'//record[./title[text()="%s"]]'

Learn more about XPath at W3Schools but note than PHP supports XPath1.0 only.

Gordon