Source of: /manual/fa/simplexmlelement.addchild.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/class.simplexmlelement.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'fa',
),
'this' =>
array (
0 => 'simplexmlelement.addchild.php',
1 => 'SimpleXMLElement::addChild',
),
'up' =>
array (
0 => 'class.simplexmlelement.php',
1 => 'SimpleXMLElement',
),
'prev' =>
array (
0 => 'simplexmlelement.addattribute.php',
1 => 'SimpleXMLElement::addAttribute',
),
'next' =>
array (
0 => 'simplexmlelement.asxml.php',
1 => 'SimpleXMLElement::asXML',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div id="simplexmlelement.addchild" class="refentry">
<div class="refnamediv">
<h1 class="refname">SimpleXMLElement::addChild</h1>
<p class="verinfo">(PHP 5 >= 5.1.3)</p><p class="refpurpose"><span class="refname">SimpleXMLElement::addChild</span> — <span class="dc-title">
Adds a child element to the XML node
</span></p>
</div>
<div class="refsect1 description" id="refsect1-simplexmlelement.addchild-description">
<h3 class="title">Description</h3>
<div class="methodsynopsis dc-description">
<span class="modifier">public</span> <span class="type"><a href="class.simplexmlelement.php" class="type SimpleXMLElement">SimpleXMLElement</a></span> <span class="methodname"><b>SimpleXMLElement::addChild</b></span>
( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$name</tt></span>
[, <span class="methodparam"><span class="type">string</span> <tt class="parameter">$value</tt></span>
[, <span class="methodparam"><span class="type">string</span> <tt class="parameter">$namespace</tt></span>
]] )</div>
<p class="para rdfs-comment">
Adds a child element to the node and returns a SimpleXMLElement of the child.
</p>
</div>
<div class="refsect1 parameters" id="refsect1-simplexmlelement.addchild-parameters">
<h3 class="title">Parameters</h3>
<p class="para">
<dl>
<dt>
<span class="term"><i><tt class="parameter">name</tt></i></span>
<dd>
<p class="para">
The name of the child element to add.
</p>
</dd>
</dt>
<dt>
<span class="term"><i><tt class="parameter">value</tt></i></span>
<dd>
<p class="para">
If specified, the value of the child element.
</p>
</dd>
</dt>
<dt>
<span class="term"><i><tt class="parameter">namespace</tt></i></span>
<dd>
<p class="para">
If specified, the namespace to which the child element belongs.
</p>
</dd>
</dt>
</dl>
</p>
</div>
<div class="refsect1 returnvalues" id="refsect1-simplexmlelement.addchild-returnvalues">
<h3 class="title">Return Values</h3>
<p class="para">
The <i>addChild</i> method returns a <span class="type"><a href="class.simplexmlelement.php" class="type SimpleXMLElement">SimpleXMLElement</a></span>
object representing the child added to the XML node.
</p>
</div>
<div class="refsect1 examples" id="refsect1-simplexmlelement.addchild-examples">
<h3 class="title">Examples</h3>
<p class="para">
<blockquote class="note"><p><b class="note">Note</b>:
<p class="para">
Listed examples may include <i>example.php</i>,
which refers to the XML string found in the first example
of the <a href="simplexml.examples-basic.php" class="link">basic usage</a> guide.
</p>
</p></blockquote>
<div class="example" id="example-4572">
<p><b>Example #1 Add attributes and children to a SimpleXML element</b></p>
<div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /><br /></span><span style="color: #007700">include </span><span style="color: #DD0000">'example.php'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$sxe </span><span style="color: #007700">= new </span><span style="color: #0000BB">SimpleXMLElement</span><span style="color: #007700">(</span><span style="color: #0000BB">$xmlstr</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$sxe</span><span style="color: #007700">-></span><span style="color: #0000BB">addAttribute</span><span style="color: #007700">(</span><span style="color: #DD0000">'type'</span><span style="color: #007700">, </span><span style="color: #DD0000">'documentary'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$movie </span><span style="color: #007700">= </span><span style="color: #0000BB">$sxe</span><span style="color: #007700">-></span><span style="color: #0000BB">addChild</span><span style="color: #007700">(</span><span style="color: #DD0000">'movie'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$movie</span><span style="color: #007700">-></span><span style="color: #0000BB">addChild</span><span style="color: #007700">(</span><span style="color: #DD0000">'title'</span><span style="color: #007700">, </span><span style="color: #DD0000">'PHP2: More Parser Stories'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$movie</span><span style="color: #007700">-></span><span style="color: #0000BB">addChild</span><span style="color: #007700">(</span><span style="color: #DD0000">'plot'</span><span style="color: #007700">, </span><span style="color: #DD0000">'This is all about the people who make it work.'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$characters </span><span style="color: #007700">= </span><span style="color: #0000BB">$movie</span><span style="color: #007700">-></span><span style="color: #0000BB">addChild</span><span style="color: #007700">(</span><span style="color: #DD0000">'characters'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$character </span><span style="color: #007700">= </span><span style="color: #0000BB">$characters</span><span style="color: #007700">-></span><span style="color: #0000BB">addChild</span><span style="color: #007700">(</span><span style="color: #DD0000">'character'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$character</span><span style="color: #007700">-></span><span style="color: #0000BB">addChild</span><span style="color: #007700">(</span><span style="color: #DD0000">'name'</span><span style="color: #007700">, </span><span style="color: #DD0000">'Mr. Parser'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$character</span><span style="color: #007700">-></span><span style="color: #0000BB">addChild</span><span style="color: #007700">(</span><span style="color: #DD0000">'actor'</span><span style="color: #007700">, </span><span style="color: #DD0000">'John Doe'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$rating </span><span style="color: #007700">= </span><span style="color: #0000BB">$movie</span><span style="color: #007700">-></span><span style="color: #0000BB">addChild</span><span style="color: #007700">(</span><span style="color: #DD0000">'rating'</span><span style="color: #007700">, </span><span style="color: #DD0000">'5'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$rating</span><span style="color: #007700">-></span><span style="color: #0000BB">addAttribute</span><span style="color: #007700">(</span><span style="color: #DD0000">'type'</span><span style="color: #007700">, </span><span style="color: #DD0000">'stars'</span><span style="color: #007700">);<br /> <br />echo </span><span style="color: #0000BB">$sxe</span><span style="color: #007700">-></span><span style="color: #0000BB">asXML</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
<div class="example-contents"><p>The above example will output
something similar to:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
<?xml version="1.0" standalone="yes"?>
<movies type="documentary">
<movie>
<title>PHP: Behind the Parser</title>
<characters>
<character>
<name>Ms. Coder</name>
<actor>Onlivia Actora</actor>
</character>
<character>
<name>Mr. Coder</name>
<actor>El Act&#xD3;r</actor>
</character>
</characters>
<plot>
So, this language. It's like, a programming language. Or is it a
scripting language? All is revealed in this thrilling horror spoof
of a documentary.
</plot>
<great-lines>
<line>PHP solves all my web problems</line>
</great-lines>
<rating type="thumbs">7</rating>
<rating type="stars">5</rating>
</movie>
<movie>
<title>PHP2: More Parser Stories</title>
<plot>This is all about the people who make it work.</plot>
<characters>
<character>
<name>Mr. Parser</name>
<actor>John Doe</actor>
</character>
</characters>
<rating type="stars">5</rating>
</movie>
</movies>
</pre></div>
</div>
</div>
</p>
</div>
<div class="refsect1 seealso" id="refsect1-simplexmlelement.addchild-seealso">
<h3 class="title">See Also</h3>
<p class="para">
<ul class="simplelist">
<li class="member"> <span class="methodname"><a href="simplexmlelement.addattribute.php" class="methodname" rel="rdfs-seeAlso">SimpleXMLElement::addAttribute()</a> - Adds an attribute to the SimpleXML element</span></li>
<li class="member"><a href="simplexml.examples-basic.php" class="xref">Basic SimpleXML usage</a></li>
</ul>
</p>
</div>
</div><?php manual_footer(); ?>