开发者

remove element from xml

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 'DOMExcep开发者_如何学Pythontion' 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


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);
}


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->preserveWhiteSpace = FALSE;
$doc->load('playlist.xml');

$xPath = new DOMXPath($doc);
$query = sprintf('//record[./title[contains(., "%s")]]', $searchString);
foreach($xPath->query($query) as $node) {
    $node->parentNode->removeChild($node);
}
$doc->formatOutput = TRUE;
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 W3C but note that PHP supports XPath1.0 only.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜