<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >DomNode->append_child</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="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=UTF-8"></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" >PHP 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.domnode-add-namespace.html" ACCESSKEY="P" >上一页</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" >下一页</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="AEN44692" ></A ><P > (no version information, might be only in CVS)</P >DomNode->append_child -- Adds a new child at the end of the children </DIV ><DIV CLASS="refsect1" ><A NAME="AEN44695" ></A ><H2 >说明</H2 >class <B CLASS="classname" >DOMNode</B > { <BR ></BR >DOMNode <B CLASS="methodname" >append_child</B > ( DOMNode newnode )<BR ></BR >}<P > This functions appends a child to an existing list of children or creates a new list of children. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN44707" ></A ><H2 >参数</H2 ><P > <P ></P ><DIV CLASS="variablelist" ><DL ><DT ><CODE CLASS="parameter" >newnode</CODE ></DT ><DD ><P > The node being appended. It can be created with e.g. <A HREF="function.domdocument-create-element.html" >DomDocument->create_element</A >, <A HREF="function.domdocument-create-text-node.html" >DomDocument->create_text_node</A > etc. or simply by using any other node. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > You can not append a <B CLASS="classname" >DOMAttribute</B > using this method. Use <A HREF="function.domelement-set-attribute.html" >DomElement->set_attribute()</A > instead. </P ></BLOCKQUOTE ></DIV ></DD ></DL ></DIV > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN44722" ></A ><H2 >返回值</H2 ><P > Returns the appended node on success or <TT CLASS="constant" ><B >FALSE</B ></TT > on failure. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN44726" ></A ><H2 >更新日志</H2 ><P > <DIV CLASS="informaltable" ><P ></P ><A NAME="AEN44729" ></A ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><THEAD ><TR ><TH >版本</TH ><TH >说明</TH ></TR ></THEAD ><TBODY ><TR ><TD >4.3.0</TD ><TD > You are not allowed anymore to insert a node from another document. </TD ></TR ><TR ><TD >4.3.0</TD ><TD > Prior to PHP 4.3.0, the new child is duplicated before being appended. 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. </TD ></TR ><TR ><TD >4.3.0 and 4.3.1</TD ><TD > The new child <CODE CLASS="parameter" >newnode</CODE > is first unlinked from its existing context, if it's already a child of DomNode. Therefore the <CODE CLASS="parameter" >newnode</CODE > is moved and not copies anymore. This is the behaviour according to the W3C specifications. If you need the old behaviour, use <A HREF="function.domnode-clone-node.html" >DomNode->clone_node</A > before appending. </TD ></TR ><TR ><TD >4.3.2</TD ><TD > The new child <CODE CLASS="parameter" >newnode</CODE > is first unlinked from its existing context, if it's already in the tree. Same rules apply. </TD ></TR ></TBODY ></TABLE ><P ></P ></DIV > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN44752" ></A ><H2 >范例</H2 ><P > The following example adds a new element node to a fresh document and sets the attribute <TT CLASS="literal" >align</TT > to <TT CLASS="literal" >left</TT >. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN44758" ></A ><P ><B >例 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 > </P ><P > The above example could also be written as the following: </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN44763" ></A ><P ><B >例 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 > </P ><P > 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. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN44768" ></A ><P ><B >例 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 /></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 /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > The above example could also be done with <A HREF="function.domnode-insert-before.html" >DomNode->insert_before</A > instead of <A HREF="function.domnode-append-child.html" >DomNode->append_child</A >. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN44774" ></A ><H2 >Migrating to PHP 5</H2 ><P > You should use <A HREF="function.dom-domnode-appendchild.html" >DOMNode->appendChild()</A >. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN44778" ></A ><H2 >参见</H2 ><P > <P ></P ><TABLE BORDER="0" ><TBODY ><TR ><TD ><A HREF="function.domnode-insert-before.html" >DomNode->insert_before</A ></TD ></TR ><TR ><TD ><A HREF="function.domnode-clone-node.html" >DomNode->clone_node</A ></TD ></TR ></TBODY ></TABLE ><P ></P > </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" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.domnode-append-sibling.html" ACCESSKEY="N" >下一页</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" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >DomNode->append_sibling</TD ></TR ></TABLE ></DIV ></BODY ></HTML >