<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = 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 => 'zh',
  ),
  'this' => 
  array (
    0 => 'simplexmlelement.addattribute.php',
    1 => 'SimpleXMLElement::addAttribute',
    2 => 'Adds an attribute to the SimpleXML element',
  ),
  'up' => 
  array (
    0 => 'class.simplexmlelement.php',
    1 => 'SimpleXMLElement',
  ),
  'prev' => 
  array (
    0 => 'class.simplexmlelement.php',
    1 => 'SimpleXMLElement',
  ),
  'next' => 
  array (
    0 => 'simplexmlelement.addchild.php',
    1 => 'SimpleXMLElement::addChild',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/simplexml/simplexmlelement/addAttribute.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="simplexmlelement.addattribute" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">SimpleXMLElement::addAttribute</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.3, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">SimpleXMLElement::addAttribute</span> &mdash; <span class="dc-title">
   Adds an attribute to the SimpleXML element
  </span></p>

 </div>
 <div class="refsect1 description" id="refsect1-simplexmlelement.addattribute-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>SimpleXMLElement::addAttribute</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$qualifiedName</code></span>, <span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$value</code></span>, <span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.string.php" class="type string">string</a></span></span> <code class="parameter">$namespace</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>): <span class="type"><a href="language.types.void.php" class="type void">void</a></span></div>

  <p class="para rdfs-comment">
   Adds an attribute to the SimpleXML element.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-simplexmlelement.addattribute-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">qualifiedName</code></dt>
     <dd>
      <p class="para">
       The name of the attribute to add.
      </p>
     </dd>
    
    
     <dt><code class="parameter">value</code></dt>
     <dd>
      <p class="para">
       The value of the attribute.
      </p>
     </dd>
    
    
     <dt><code class="parameter">namespace</code></dt>
     <dd>
      <p class="para">
       If specified, the namespace to which the attribute belongs.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-simplexmlelement.addattribute-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   没有返回值。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-simplexmlelement.addattribute-examples">
  <h3 class="title">示例</h3>
  <p class="para">
   <blockquote class="note"><p><strong class="note">注意</strong>: 
    <p class="para">
     Listed examples may include <code class="literal">examples/simplexml-data.php</code>,
     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-1">
    <p><strong>示例 #1 Add attributes and children to a SimpleXML element</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">include </span><span style="color: #DD0000">'examples/simplexml-data.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">-&gt;</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">-&gt;</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">-&gt;</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">-&gt;</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">-&gt;</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">-&gt;</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">-&gt;</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">-&gt;</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">-&gt;</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">-&gt;</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">-&gt;</span><span style="color: #0000BB">asXML</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>以上示例的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">&lt;?xml version=&quot;1.0&quot; standalone=&quot;yes&quot;?&gt;
&lt;movies type=&quot;documentary&quot;&gt;
 &lt;movie&gt;
  &lt;title&gt;PHP: Behind the Parser&lt;/title&gt;
  &lt;characters&gt;
   &lt;character&gt;
    &lt;name&gt;Ms. Coder&lt;/name&gt;
    &lt;actor&gt;Onlivia Actora&lt;/actor&gt;
   &lt;/character&gt;
   &lt;character&gt;
    &lt;name&gt;Mr. Coder&lt;/name&gt;
    &lt;actor&gt;El Act&amp;#xD3;r&lt;/actor&gt;
   &lt;/character&gt;
  &lt;/characters&gt;
  &lt;plot&gt;
   So, this language. It&#039;s like, a programming language. Or is it a
   scripting language? All is revealed in this thrilling horror spoof
   of a documentary.
  &lt;/plot&gt;
  &lt;great-lines&gt;
   &lt;line&gt;PHP solves all my web problems&lt;/line&gt;
  &lt;/great-lines&gt;
  &lt;rating type=&quot;thumbs&quot;&gt;7&lt;/rating&gt;
  &lt;rating type=&quot;stars&quot;&gt;5&lt;/rating&gt;
 &lt;/movie&gt;
 &lt;movie&gt;
  &lt;title&gt;PHP2: More Parser Stories&lt;/title&gt;
  &lt;plot&gt;This is all about the people who make it work.&lt;/plot&gt;
  &lt;characters&gt;
   &lt;character&gt;
    &lt;name&gt;Mr. Parser&lt;/name&gt;
    &lt;actor&gt;John Doe&lt;/actor&gt;
   &lt;/character&gt;
  &lt;/characters&gt;
  &lt;rating type=&quot;stars&quot;&gt;5&lt;/rating&gt;
 &lt;/movie&gt;
&lt;/movies&gt;</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-simplexmlelement.addattribute-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="methodname"><a href="simplexmlelement.addchild.php" class="methodname" rel="rdfs-seeAlso">SimpleXMLElement::addChild()</a> - Adds a child element to the XML node</span></li>
    <li><a href="simplexml.examples-basic.php" class="xref">Basic SimpleXML usage</a></li>
   </ul>
  </p>
 </div>

</div><?php manual_footer($setup); ?>