<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >DomNode->append_child</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Manual PHP" HREF="index.html"><LINK REL="UP" TITLE="DOM XML functions" HREF="ref.domxml.html"><LINK REL="PREVIOUS" TITLE="DomNode->add_namespace" HREF="function.domnode-add-namespace.html"><LINK REL="NEXT" TITLE="DomNode->append_sibling" HREF="function.domnode-append-sibling.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-2"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Manual PHP</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.domnode-add-namespace.html" ACCESSKEY="P" >Înapoi</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.domnode-append-sibling.html" ACCESSKEY="N" >Înainte</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.domnode-append-child" ></A >DomNode->append_child</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN23698" ></A ><P > (no version information, might be only in CVS)</P >DomNode->append_child -- Adds new child at the end of the children </DIV ><DIV CLASS="refsect1" ><A NAME="AEN23701" ></A ><H2 >Description</H2 >object <B CLASS="methodname" >DomNode->append_child</B > ( object newnode)<BR ></BR ><P > 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. <A HREF="function.domdocument-create-element.html" ><B CLASS="function" >domdocument_create_element()</B ></A >, <B CLASS="function" >domdocument_create_text()</B > etc. or simply by using any other node. </P ><P > (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 an 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. </P ><P > (PHP 4.3.0/4.3.1) The new child <VAR CLASS="parameter" >newnode</VAR > is first unlinked from its existing context, if it's already a child of DomNode. Therefore the node is moved and not copies anymore. </P ><P > (PHP >= 4.3.2) The new child <VAR CLASS="parameter" >newnode</VAR > 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 an XML document, use DomNode->clone_node() before appending. </P ><P > The following example will add a new element node to a fresh document and sets the attribute "align" to "left". <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN23718" ></A ><P ><B >Exemplu 1. Adding a child</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$doc </font><font color="#007700">= </font><font color="#0000BB">domxml_new_doc</font><font color="#007700">(</font><font color="#DD0000">"1.0"</font><font color="#007700">);<br /></font><font color="#0000BB">$node </font><font color="#007700">= </font><font color="#0000BB">$doc</font><font color="#007700">-></font><font color="#0000BB">create_element</font><font color="#007700">(</font><font color="#DD0000">"para"</font><font color="#007700">);<br /></font><font color="#0000BB">$newnode </font><font color="#007700">= </font><font color="#0000BB">$doc</font><font color="#007700">-></font><font color="#0000BB">append_child</font><font color="#007700">(</font><font color="#0000BB">$node</font><font color="#007700">);<br /></font><font color="#0000BB">$newnode</font><font color="#007700">-></font><font color="#0000BB">set_attribute</font><font color="#007700">(</font><font color="#DD0000">"align"</font><font color="#007700">, </font><font color="#DD0000">"left"</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > The above example could also be written as the following: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN23721" ></A ><P ><B >Exemplu 2. Adding a child</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$doc </font><font color="#007700">= </font><font color="#0000BB">domxml_new_doc</font><font color="#007700">(</font><font color="#DD0000">"1.0"</font><font color="#007700">);<br /></font><font color="#0000BB">$node </font><font color="#007700">= </font><font color="#0000BB">$doc</font><font color="#007700">-></font><font color="#0000BB">create_element</font><font color="#007700">(</font><font color="#DD0000">"para"</font><font color="#007700">);<br /></font><font color="#0000BB">$node</font><font color="#007700">-></font><font color="#0000BB">set_attribute</font><font color="#007700">(</font><font color="#DD0000">"align"</font><font color="#007700">, </font><font color="#DD0000">"left"</font><font color="#007700">);<br /></font><font color="#0000BB">$newnode </font><font color="#007700">= </font><font color="#0000BB">$doc</font><font color="#007700">-></font><font color="#0000BB">append_child</font><font color="#007700">(</font><font color="#0000BB">$node</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > A more complex 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. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN23724" ></A ><P ><B >Exemplu 3. Adding a child</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">include(</font><font color="#DD0000">"example.inc"</font><font color="#007700">);<br /><br />if (!</font><font color="#0000BB">$dom </font><font color="#007700">= </font><font color="#0000BB">domxml_open_mem</font><font color="#007700">(</font><font color="#0000BB">$xmlstr</font><font color="#007700">)) {<br /> echo </font><font color="#DD0000">"Error while parsing the document\n"</font><font color="#007700">;<br /> exit;<br />}<br /><br /></font><font color="#0000BB">$elements </font><font color="#007700">= </font><font color="#0000BB">$dom</font><font color="#007700">-></font><font color="#0000BB">get_elements_by_tagname</font><font color="#007700">(</font><font color="#DD0000">"informaltable"</font><font color="#007700">);<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$elements</font><font color="#007700">);<br /></font><font color="#0000BB">$element </font><font color="#007700">= </font><font color="#0000BB">$elements</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">];<br /><br /></font><font color="#0000BB">$parent </font><font color="#007700">= </font><font color="#0000BB">$element</font><font color="#007700">-></font><font color="#0000BB">parent_node</font><font color="#007700">();<br /></font><font color="#0000BB">$newnode </font><font color="#007700">= </font><font color="#0000BB">$parent</font><font color="#007700">-></font><font color="#0000BB">append_child</font><font color="#007700">(</font><font color="#0000BB">$element</font><font color="#007700">);<br /></font><font color="#0000BB">$children </font><font color="#007700">= </font><font color="#0000BB">$newnode</font><font color="#007700">-></font><font color="#0000BB">children</font><font color="#007700">();<br /></font><font color="#0000BB">$attr </font><font color="#007700">= </font><font color="#0000BB">$children</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">]-></font><font color="#0000BB">set_attribute</font><font color="#007700">(</font><font color="#DD0000">"align"</font><font color="#007700">, </font><font color="#DD0000">"left"</font><font color="#007700">);<br /><br />echo </font><font color="#DD0000">"<pre>"</font><font color="#007700">;<br /></font><font color="#0000BB">$xmlfile </font><font color="#007700">= </font><font color="#0000BB">$dom</font><font color="#007700">-></font><font color="#0000BB">dump_mem</font><font color="#007700">();<br />echo </font><font color="#0000BB">htmlentities</font><font color="#007700">(</font><font color="#0000BB">$xmlfile</font><font color="#007700">);<br />echo </font><font color="#DD0000">"</pre>"</font><font color="#007700">;<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > The above example could also be done with <A HREF="function.domnode-insert-before.html" ><B CLASS="function" >domnode_insert_before()</B ></A > instead of <A HREF="function.domnode-append-child.html" ><B CLASS="function" >domnode_append_child()</B ></A >. </P ><P > See also <A HREF="function.domnode-insert-before.html" ><B CLASS="function" >domnode_insert_before()</B ></A >, and <A HREF="function.domnode-clone-node.html" ><B CLASS="function" >domnode_clone_node()</B ></A >. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.domnode-add-namespace.html" ACCESSKEY="P" >Înapoi</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Acasã</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.domnode-append-sibling.html" ACCESSKEY="N" >Înainte</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >DomNode->add_namespace</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.domxml.html" ACCESSKEY="U" >Sus</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >DomNode->append_sibling</TD ></TR ></TABLE ></DIV ></BODY ></HTML >