(no version information, might be only in CVS)
DomNode_>append_child __
Adds new child at the end of the children
Description
object
DomNode_>append_child ( object newnode)
This functions appends a child to an existing list of children or creates
a new list of children_ The child can be created with e_g_
domdocument_create_element(),
domdocument_create_text() etc_ or simply by using any
other node_
(PHP < 4_3) Before a new child is appended it is first duplicated_ Therefore the new
child is a completely new copy which can be modified without changing the
node which was passed to this function_ If the node passed has children
itself, they will be duplicated as well, which makes it quite easy to
duplicate large parts of a xml document_ The return value is the
appended child_ If you plan to do further modifications on the appended
child you must use the returned node_
(PHP 4_3_0/4_3_1) The new child newnode is first
unlinked from its existing context, if it's already a child of DomNode_
Therefore the node is moved and not copies anymore_
(PHP >= 4_3_2) The new child newnode is first
unlinked from its existing context, if it's already in the tree_ Therefore
the node is moved and not copied_ This is the behaviour according to the
W3C specifications_ If you want to duplicate large parts of a xml document,
use DomNode_>clone_node() before appending_
The following example will add a new element node to a fresh document
and sets the attribute "align" to "left"_
Ejemplo 1_ Adding a child <?php
$doc = domxml_new_doc("1_0");
$node = $doc_>create_element("para");
$newnode = $doc_>append_child($node);
$newnode_>set_attribute("align", "left");
?> |
|
The above example could also be written as the following:
Ejemplo 2_ Adding a child <?php
$doc = domxml_new_doc("1_0");
$node = $doc_>create_element("para");
$node_>set_attribute("align", "left");
$newnode = $doc_>append_child($node);
?> |
|
A more comples example is the one below_ It first searches for a certain
element, duplicates it including its children and adds it as a sibling_
Finally a new attribute is added to one of the children of the new
sibling and the whole document is dumped_
Ejemplo 3_ Adding a child <?php
include("example_inc");
if(!$dom = domxml_open_mem($xmlstr)) {
echo "Error while parsing the document\n";
exit;
}
$elements = $dom_>get_elements_by_tagname("informaltable");
print_r($elements);
$element = $elements[0];
$parent = $element_>parent_node();
$newnode = $parent_>append_child($element);
$children = $newnode_>children();
$attr = $children[1]_>set_attribute("align", "left");
echo "<PRE>";
$xmlfile = $dom_>dump_mem();
echo htmlentities($xmlfile);
echo "</PRE>";
?> |
|
The above example could also be done with
domnode_insert_before() instead of
domnode_append_child()_
See also domnode_insert_before(),
domnode_clone_node()_