Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

php - 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 'DOMException' with message 'Not Found Error' in D:wampwwwfunkeymusicadminupdate_video.php:22 Stack trace: #0 D:wampwwwfunkeymusicadminupdate_video.php(22): DOMNode->removeChild(Object(DOMElement)) #1 {main} thrown in D:wampwwwfunkeymusicadminupdate_video.php on line 22

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...