(PHP 5)
DOMNode::removeChild — Removes child from list of children
This functions removes a child from a list of children.
The removed child.
If the child could be removed the functions returns the old child.
Raised if this node is readonly.
Raised if oldnode is not a child of this node.
The following example will delete the chapter element of our XML document.
Пример #1 Removing a child
<?php
$doc = new DOMDocument;
$doc->load('book.xml');
$book = $doc->documentElement;
// we retrieve the chapter and remove it from the book
$chapter = $book->getElementsByTagName('chapter')->item(0);
$oldchapter = $book->removeChild($chapter);
echo $doc->saveXML();
?>
Результат выполнения данного примера:
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN" "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd"> <book id="listing"> <title>My lists</title> </book>
Пример #2 Preserving the parent node's namespace URI
<?php
$doc = new DOMDocument;
$doc->load('book.xml');
$book = $doc->documentElement;
// we retrieve the chapter and remove it from the book
$chapter = $book->getElementsByTagName('chapter')->item(0);
// copy the namespace URI
$nsuri = $book->namespaceURI;
// Delete the child node
$book->removeChild($chapter);
// paste the namespace URI back into the parent node
$book->namespaceURI = $nsuri;
?>
Замечание:
After calling this method, the DOMNode::$namespaceURI property on the parent node will be reset to NULL. Above example shows how to work around it.