根据索引删除xml个元素节点
Remove xml element node based on index
我有一个包含许多元素的 xml 文件,我希望根据索引删除某些元素。
样本XML:
<songs>
<song>
<title>A</title>
<artist>AA</artist>
</song>
</songs>
PHP 目前我的代码:
<?php
$index = $_GET['song'];
$doc = new DOMDocument();
$doc->load("songs.xml");
$songs = $doc->getElementsByTagName("song");
foreach($songs as $song) {
if(in_array($song[$index], $songs)){
unset($song);
}
}
$doc->save("songlist.xml");
?>
但是我从这段代码中得到了这个错误:
Fatal error: Cannot use object of type DOMElement as array
在此问题上提供任何帮助,谢谢。
$songs = $doc->getElementsByTagName('song');
$songs.item(1).parentNode.removeChild($songs.item(1));
$songs.item(7).parentNode.removeChild($songs.item(7));
$songs.item(12).parentNode.removeChild($songs.item(12));
等...
如果您打算删除整个 <song>
元素。使用 ->removeChild()
:
$songs = $doc->getElementsByTagName('song');
$hated_artists = array('AA', 'BB', 'CC');
foreach($songs as $song) {
$artist = $song->getElementsByTagName('artist')->item(0)->nodeValue;
if(in_array($artist, $hated_artists)) { // if this song is sang by one of your hated artists
$song->parentNode->removeChild($song); // remove this song
}
}
如果您只想要一个简单的标准,通过您想要的键删除,那么只需使用 foreach
键。示例:
$keys_to_be_removed = array(14, 18);
foreach($songs as $key => $song) {
if(in_array($key, $keys_to_be_removed)) {
$song->parentNode->removeChild($song);
}
}
或者只是显式地使用索引删除而不使用 foreach 循环:
// starts at index zero
$first_song = $doc->getElementsByTagName('song')->item(0);
$songs = $doc->documentElement;
$songs->removeChild($first_song);
XPath 可以 select 节点 index/position(以 1 开头)。它总是 return 一个可以用 foreach 遍历的节点列表。如果索引不存在,列表将为空。要从 DOM 中删除节点,您必须将其从其父节点中删除。
$xml = <<<'XML'
<songs>
<song>
<title>A</title>
<artist>AA</artist>
</song>
</songs>
XML;
$index = 1; // (int)$_GET['song'];
$doc = new DOMDocument();
$doc->loadXml($xml);
$xpath = new DOMXPath($doc);
foreach ($xpath->evaluate('//song['.$index.']') as $song) {
$song->parentNode->removeChild($song);
}
echo $doc->saveXml();
输出:
<?xml version="1.0"?>
<songs>
</songs>
我有一个包含许多元素的 xml 文件,我希望根据索引删除某些元素。
样本XML:
<songs>
<song>
<title>A</title>
<artist>AA</artist>
</song>
</songs>
PHP 目前我的代码:
<?php
$index = $_GET['song'];
$doc = new DOMDocument();
$doc->load("songs.xml");
$songs = $doc->getElementsByTagName("song");
foreach($songs as $song) {
if(in_array($song[$index], $songs)){
unset($song);
}
}
$doc->save("songlist.xml");
?>
但是我从这段代码中得到了这个错误:
Fatal error: Cannot use object of type DOMElement as array
在此问题上提供任何帮助,谢谢。
$songs = $doc->getElementsByTagName('song');
$songs.item(1).parentNode.removeChild($songs.item(1));
$songs.item(7).parentNode.removeChild($songs.item(7));
$songs.item(12).parentNode.removeChild($songs.item(12));
等...
如果您打算删除整个 <song>
元素。使用 ->removeChild()
:
$songs = $doc->getElementsByTagName('song');
$hated_artists = array('AA', 'BB', 'CC');
foreach($songs as $song) {
$artist = $song->getElementsByTagName('artist')->item(0)->nodeValue;
if(in_array($artist, $hated_artists)) { // if this song is sang by one of your hated artists
$song->parentNode->removeChild($song); // remove this song
}
}
如果您只想要一个简单的标准,通过您想要的键删除,那么只需使用 foreach
键。示例:
$keys_to_be_removed = array(14, 18);
foreach($songs as $key => $song) {
if(in_array($key, $keys_to_be_removed)) {
$song->parentNode->removeChild($song);
}
}
或者只是显式地使用索引删除而不使用 foreach 循环:
// starts at index zero
$first_song = $doc->getElementsByTagName('song')->item(0);
$songs = $doc->documentElement;
$songs->removeChild($first_song);
XPath 可以 select 节点 index/position(以 1 开头)。它总是 return 一个可以用 foreach 遍历的节点列表。如果索引不存在,列表将为空。要从 DOM 中删除节点,您必须将其从其父节点中删除。
$xml = <<<'XML'
<songs>
<song>
<title>A</title>
<artist>AA</artist>
</song>
</songs>
XML;
$index = 1; // (int)$_GET['song'];
$doc = new DOMDocument();
$doc->loadXml($xml);
$xpath = new DOMXPath($doc);
foreach ($xpath->evaluate('//song['.$index.']') as $song) {
$song->parentNode->removeChild($song);
}
echo $doc->saveXml();
输出:
<?xml version="1.0"?>
<songs>
</songs>