从xml中删除元素 [英] remove element from xml
问题描述
在我的xml文件中,我想根据标题删除记录元素 我的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>
我的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中堆栈错误:未捕获的异常'DOMException',消息为'Not Found Error':22堆栈跟踪:#0 D:\ wamp \ www \ funkeymusic \ admin \ update_video.php(22):DOMNode-> removeChild(Object(DOMElement))#1 {main}在第22行的D:\ wamp \ www \ funkeymusic \ admin \ update_video.php中抛出
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
推荐答案
从我理解的问题开始,您想使用包含特定文本的<title>
子元素删除<record>
元素.解决方案比它需要的更为冗长,并且$attrValue
建议title元素的DOMText是Attribute,它不是.但是无论如何,让我们删除所有这些并使用XPath:
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();
XPath说,找到所有记录节点,这些记录节点的子标题带有包含搜索字符串的文本节点.请注意,包含,并不意味着等于,因此,如果将"Title:"用作$searchString
,它将删除除"The_Princess_And_The_Frog_HD"以外的所有电影.如果要删除确切的标题,请将XPath更改为
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"]]'
了解有关 W3C上的XPath 的更多信息,但请注意,PHP仅支持XPath1.0.
Learn more about XPath at W3C but note that PHP supports XPath1.0 only.
这篇关于从xml中删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!