从XML

2010-07-23 23 views
1

在我的XML文件中删除元素,我想根据标题去掉记录元素 我的XML文件是从XML

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

和我的PHP文件是

 <?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(); 
         ?> 

它提供了以下错误: -

致命错误:在D:\ wamp \ www \ funkeymusic \ admin \ update_video.php:22堆栈跟踪:#0 D:\ wamp \ www \ funkeymusic \中找到未找到异常'DOMException'管理员\ updat第22行D:\ wamp \ www \ funkeymusic \ admin \ update_video.php中抛出的DOMNode-> removeChild(Object(DOMElement))#1 {main}

回答

4

您只能致电removeChild()在相应的父节点上。由于$nodeToRemove不是$thedocument(它是后代)的直接子代,因此会出现“未找到”错误。

if ($nodeToRemove != null) { 
    $nodeToRemove->parentNode->removeChild($nodeToRemove); 
} 
+0

但它不是从文件中删除 – Rajanikant 2010-07-23 12:10:49

+0

@Rajanikant:它做了什么呢? – Tomalak 2010-07-23 12:15:00

+0

它显示空白页面或磨打印 – Rajanikant 2010-07-23 12:19:53

4

从这个问题我明白你要删除的<record>元素与<title>孩子所含有的特殊文本。解决方案比需要的更详细,$attrValue暗示标题元素的DOMText是属性,它不是。但无论如何,我们删除了这一切,使用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(); 

中的XPath说,找到所有纪录节点都包含搜索字符串文本节点的子标题。请注意,包含,并不意味着等于,所以如果您使用“Title:”作为$searchString,它将删除除“The_Princess_And_The_Frog_HD”之外的所有电影。如果你想删除确切的冠军,XPath的改变

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

了解更多关于XPath at W3C但请注意,PHP只支持XPath1.0。