<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/simplexml.examples.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'es',
  ),
  'this' => 
  array (
    0 => 'simplexml.examples-basic.php',
    1 => 'Uso b&aacute;sico de SimpleXML',
    2 => 'Uso b&aacute;sico de SimpleXML',
  ),
  'up' => 
  array (
    0 => 'simplexml.examples.php',
    1 => 'Ejemplos',
  ),
  'prev' => 
  array (
    0 => 'simplexml.examples.php',
    1 => 'Ejemplos',
  ),
  'next' => 
  array (
    0 => 'simplexml.examples-errors.php',
    1 => 'Manejo de errores XML',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'es',
    'path' => 'reference/simplexml/examples.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="simplexml.examples-basic" class="section">
  <h2 class="title">Uso básico de SimpleXML</h2>
  <p class="para">
   Varios ejemplos de este capítulo requieren una cadena XML. En lugar de
   repetirla en cada ejemplo, se colocará en un archivo que se incluirá en
   cada uno de ellos. El contenido de este archivo se ilustra con el ejemplo
   que sigue. De lo contrario, puede crearse un documento XML y leerse con
   <span class="function"><a href="function.simplexml-load-file.php" class="function">simplexml_load_file()</a></span>.
  </p>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Ejemplo #1 Ejemplo de archivo incluido examples/simplexml-data.php con una cadena XML</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$xmlstr </span><span style="color: #007700">= &lt;&lt;&lt;XML<br /></span><span style="color: #DD0000">&lt;?xml version='1.0' standalone='yes'?&gt;<br />&lt;movies&gt;<br /> &lt;movie&gt;<br />  &lt;title&gt;PHP: Behind the Parser&lt;/title&gt;<br />  &lt;characters&gt;<br />   &lt;character&gt;<br />    &lt;name&gt;Ms. Coder&lt;/name&gt;<br />    &lt;actor&gt;Onlivia Actora&lt;/actor&gt;<br />   &lt;/character&gt;<br />   &lt;character&gt;<br />    &lt;name&gt;Mr. Coder&lt;/name&gt;<br />    &lt;actor&gt;El Act&amp;#211;r&lt;/actor&gt;<br />   &lt;/character&gt;<br />  &lt;/characters&gt;<br />  &lt;plot&gt;<br />   So, this language. It's like, a programming language. Or is it a<br />   scripting language? All is revealed in this thrilling horror spoof<br />   of a documentary.<br />  &lt;/plot&gt;<br />  &lt;great-lines&gt;<br />   &lt;line&gt;PHP solves all my web problems&lt;/line&gt;<br />  &lt;/great-lines&gt;<br />  &lt;rating type="thumbs"&gt;7&lt;/rating&gt;<br />  &lt;rating type="stars"&gt;5&lt;/rating&gt;<br /> &lt;/movie&gt;<br />&lt;/movies&gt;<br /></span><span style="color: #007700">XML;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
  <p class="para">
   La simplicidad de SimpleXML se hace más evidente cuando se intenta
   extraer una cadena o un número de un documento XML básico.
   <div class="example" id="example-2">
    <p><strong>Ejemplo #2 Lectura de <code class="literal">&lt;plot&gt;</code></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<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">$movies </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 /><br />echo </span><span style="color: #0000BB">$movies</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">movie</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">plot</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>El ejemplo anterior mostrará:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>

   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.

</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   El acceso a los elementos de un documento XML que contiene caracteres no permitidos
   según la convención de nombres de PHP (por ejemplo, palabras clave) es posible
   encapsulando el nombre del elemento entre corchetes y comillas simples.
   <div class="example" id="example-3">
    <p><strong>Ejemplo #3 Recuperación de <code class="literal">&lt;line&gt;</code></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<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">$movies </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 /><br />echo </span><span style="color: #0000BB">$movies</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">movie</span><span style="color: #007700">-&gt;{</span><span style="color: #DD0000">'great-lines'</span><span style="color: #007700">}-&gt;</span><span style="color: #0000BB">line</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>El ejemplo anterior mostrará:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
PHP solves all my web problems
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4">
    <p><strong>Ejemplo #4 Acceder a un elemento no único con SimpleXML</strong></p>
    <div class="example-contents"><p>
     Cuando existen múltiples instancias de un elemento como hijos de un
     elemento padre único, pueden aplicarse las técnicas normales de iteración.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<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">$movies </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 /><br /></span><span style="color: #FF8000">/* Para cada &lt;character&gt;, se muestra un &lt;name&gt;. */<br /></span><span style="color: #007700">foreach (</span><span style="color: #0000BB">$movies</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">movie</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">characters</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">character </span><span style="color: #007700">as </span><span style="color: #0000BB">$character</span><span style="color: #007700">) {<br />   echo </span><span style="color: #0000BB">$character</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">name</span><span style="color: #007700">, </span><span style="color: #DD0000">' played by '</span><span style="color: #007700">, </span><span style="color: #0000BB">$character</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">actor</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>El ejemplo anterior mostrará:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Ms. Coder played by Onlivia Actora
Mr. Coder played by El ActÓr
</pre></div>
    </div>
   </div>
  </p>
  <blockquote class="note"><p><strong class="note">Nota</strong>: 
   <p class="para">
    Las propiedades (<code class="literal">$movies-&gt;movie</code> en nuestro ejemplo anterior)
    no son arrays. Son objetos
    <a href="class.iterator.php" class="link">iterables</a> y
    <a href="class.arrayaccess.php" class="link">accesibles</a>.
   </p>
  </p></blockquote>
  <p class="para">
   <div class="example" id="example-5">
    <p><strong>Ejemplo #5 Uso de atributos</strong></p>
    <div class="example-contents"><p>
     Hasta ahora, solo se ha cubierto la lectura de los nombres de los elementos
     y sus valores. SimpleXML también puede acceder a sus atributos.
     El acceso a los atributos de un elemento se realiza de la misma manera que
     el acceso a los elementos de un array.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<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">$movies </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 /><br /></span><span style="color: #FF8000">/* Acceso al nodo &lt;rating&gt; del primer &lt;movie&gt;.<br /> * Mostrar también los atributos de &lt;rating&gt;. */<br /></span><span style="color: #007700">foreach (</span><span style="color: #0000BB">$movies</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">movie</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">rating </span><span style="color: #007700">as </span><span style="color: #0000BB">$rating</span><span style="color: #007700">) {<br />    switch((string) </span><span style="color: #0000BB">$rating</span><span style="color: #007700">[</span><span style="color: #DD0000">'type'</span><span style="color: #007700">]) { </span><span style="color: #FF8000">// Obtener atributos como índices de elementos<br />    </span><span style="color: #007700">case </span><span style="color: #DD0000">'thumbs'</span><span style="color: #007700">:<br />        echo </span><span style="color: #0000BB">$rating</span><span style="color: #007700">, </span><span style="color: #DD0000">' thumbs up'</span><span style="color: #007700">;<br />        break;<br />    case </span><span style="color: #DD0000">'stars'</span><span style="color: #007700">:<br />        echo </span><span style="color: #0000BB">$rating</span><span style="color: #007700">, </span><span style="color: #DD0000">' stars'</span><span style="color: #007700">;<br />        break;<br />    }<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>El ejemplo anterior mostrará:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
7 thumbs up5 stars
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-6">
    <p><strong>Ejemplo #6 Comparación de elementos y atributos con texto</strong></p>
    <div class="example-contents"><p>
     Para comparar un elemento o un atributo con una cadena de caracteres
     o para pasarlo a una función que requiera una cadena de caracteres,
     debe convertirse en una cadena utilizando
     <code class="literal">(string)</code>.
     De lo contrario, PHP tratará el elemento como un objeto.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<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">$movies </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 /><br />if ((string) </span><span style="color: #0000BB">$movies</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">movie</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">title </span><span style="color: #007700">== </span><span style="color: #DD0000">'PHP: Behind the Parser'</span><span style="color: #007700">) {<br />    print </span><span style="color: #DD0000">'My favorite movie.'</span><span style="color: #007700">;<br />}<br /><br />echo </span><span style="color: #0000BB">htmlentities</span><span style="color: #007700">((string) </span><span style="color: #0000BB">$movies</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">movie</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">title</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>El ejemplo anterior mostrará:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
My favorite movie.PHP: Behind the Parser
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-7">
    <p><strong>Ejemplo #7 Comparación de 2 elementos</strong></p>
    <div class="example-contents"><p>
     Dos objetos <span class="classname"><a href="class.simplexmlelement.php" class="classname">SimpleXMLElement</a></span>
     se consideran diferentes incluso si
     apuntan al mismo elemento.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<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">$movies1 </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">$movies2 </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">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$movies1 </span><span style="color: #007700">== </span><span style="color: #0000BB">$movies2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>El ejemplo anterior mostrará:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
bool(false)
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-8">
    <p><strong>Ejemplo #8 Uso de XPath</strong></p>
    <div class="example-contents"><p>
     SimpleXML incluye soporte integrado para <abbr title="XML Path Language">XPath</abbr>.
     Para encontrar todos los elementos <code class="literal">&lt;character&gt;</code>.
    </p></div>
    <div class="example-contents"><p>
     &#039;<code class="literal">//</code>&#039; actúa como comodín. Para especificar una ruta absoluta,
     se elimina una barra.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<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">$movies </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 /><br />foreach (</span><span style="color: #0000BB">$movies</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">xpath</span><span style="color: #007700">(</span><span style="color: #DD0000">'//character'</span><span style="color: #007700">) as </span><span style="color: #0000BB">$character</span><span style="color: #007700">) {<br />    echo </span><span style="color: #0000BB">$character</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">name</span><span style="color: #007700">, </span><span style="color: #DD0000">' played by '</span><span style="color: #007700">, </span><span style="color: #0000BB">$character</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">actor</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>El ejemplo anterior mostrará:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Ms. Coder played by Onlivia Actora
Mr. Coder played by El ActÓr
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-9">
    <p><strong>Ejemplo #9 Asignación de valores</strong></p>
    <div class="example-contents"><p>
     Los datos en SimpleXML no tienen que ser constantes. El objeto permite
     la manipulación de todos estos elementos.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">include </span><span style="color: #DD0000">'examples/simplexml-data.php'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$movies </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 /><br /></span><span style="color: #0000BB">$movies</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">movie</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">characters</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">character</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">name </span><span style="color: #007700">= </span><span style="color: #DD0000">'Miss Coder'</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #0000BB">$movies</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">asXML</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>El ejemplo anterior mostrará:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
&lt;?xml version=&quot;1.0&quot; standalone=&quot;yes&quot;?&gt;
&lt;movies&gt;
 &lt;movie&gt;
  &lt;title&gt;PHP: Behind the Parser&lt;/title&gt;
  &lt;characters&gt;
   &lt;character&gt;
    &lt;name&gt;Miss 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;/movies&gt;
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-10">
    <p><strong>Ejemplo #10 Añadir elementos y atributos</strong></p>
    <div class="example-contents"><p>
     SimpleXML es capaz de añadir fácilmente
     hijos y atributos.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">include </span><span style="color: #DD0000">'examples/simplexml-data.php'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$movies </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 /><br /></span><span style="color: #0000BB">$character </span><span style="color: #007700">= </span><span style="color: #0000BB">$movies</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">movie</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-&gt;</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">$movies</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">movie</span><span style="color: #007700">[</span><span style="color: #0000BB">0</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">'PG'</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">'mpaa'</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #0000BB">$movies</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">asXML</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>El ejemplo anterior mostrará:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
&lt;?xml version=&quot;1.0&quot; standalone=&quot;yes&quot;?&gt;
&lt;movies&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;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;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;rating type=&quot;mpaa&quot;&gt;PG&lt;/rating&gt;&lt;/movie&gt;
&lt;/movies&gt;
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-11">
    <p><strong>Ejemplo #11 Interoperabilidad DOM</strong></p>
    <div class="example-contents"><p>
     PHP tiene un mecanismo para convertir nodos XML entre los formatos
     SimpleXML y DOM. Este ejemplo muestra cómo cambiar un elemento DOM a
     SimpleXML.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$dom </span><span style="color: #007700">= new </span><span style="color: #0000BB">DOMDocument</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$dom</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">loadXML</span><span style="color: #007700">(</span><span style="color: #DD0000">'&lt;books&gt;&lt;book&gt;&lt;title&gt;blah&lt;/title&gt;&lt;/book&gt;&lt;/books&gt;'</span><span style="color: #007700">);<br />if (!</span><span style="color: #0000BB">$dom</span><span style="color: #007700">) {<br />    echo </span><span style="color: #DD0000">'Error al analizar el documento'</span><span style="color: #007700">;<br />    exit;<br />}<br /><br /></span><span style="color: #0000BB">$books </span><span style="color: #007700">= </span><span style="color: #0000BB">simplexml_import_dom</span><span style="color: #007700">(</span><span style="color: #0000BB">$dom</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #0000BB">$books</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">book</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">title</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>El ejemplo anterior mostrará:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
blah
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-12">
    <p><strong>Ejemplo #12 Uso de espacios de nombres</strong></p>
    <div class="example-contents">
     <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$data </span><span style="color: #007700">= &lt;&lt;&lt;XML<br /></span><span style="color: #DD0000">&lt;movies xmlns="http://default" xmlns:a="http://a"&gt;<br /> &lt;movie xml:id="movie1" a:link="IMDB"&gt;<br />  &lt;a:actor&gt;Onlivia Actora&lt;/a:actor&gt;<br /> &lt;/movie&gt;<br />&lt;/movies&gt;<br /></span><span style="color: #007700">XML;<br /><br /></span><span style="color: #0000BB">$movies </span><span style="color: #007700">= </span><span style="color: #0000BB">simplexml_load_string</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// El espacio de nombres http://www.w3.org/XML/1998/namespace está disponible bajo el nombre "xml".<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$movies</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">movie</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">attributes</span><span style="color: #007700">(</span><span style="color: #DD0000">"xml"</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">)[</span><span style="color: #DD0000">"id"</span><span style="color: #007700">] . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Los atributos con espacio de nombres pueden recuperarse con attributes().<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$movies</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">movie</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">attributes</span><span style="color: #007700">(</span><span style="color: #DD0000">"a"</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">)[</span><span style="color: #DD0000">"link"</span><span style="color: #007700">] . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// El uso de la URI del espacio de nombres permite usar cualquier alias en el documento.<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$movies</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">movie</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">attributes</span><span style="color: #007700">(</span><span style="color: #DD0000">"http://a"</span><span style="color: #007700">)[</span><span style="color: #DD0000">"link"</span><span style="color: #007700">] . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Los hijos pueden recuperarse con children().<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$movies</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">movie</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">children</span><span style="color: #007700">(</span><span style="color: #DD0000">"http://a"</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">actor </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// El uso de xpath() con un espacio de nombres requiere registrarlo primero.<br /></span><span style="color: #0000BB">$movies</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">registerXPathNamespace</span><span style="color: #007700">(</span><span style="color: #DD0000">"a"</span><span style="color: #007700">, </span><span style="color: #DD0000">"http://a"</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$movies</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">xpath</span><span style="color: #007700">(</span><span style="color: #DD0000">"//a:actor"</span><span style="color: #007700">)) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Incluso el espacio de nombres por defecto debe registrarse.<br /></span><span style="color: #0000BB">$movies</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">registerXPathNamespace</span><span style="color: #007700">(</span><span style="color: #DD0000">"default"</span><span style="color: #007700">, </span><span style="color: #DD0000">"http://default"</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$movies</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">xpath</span><span style="color: #007700">(</span><span style="color: #DD0000">"//default:movie"</span><span style="color: #007700">)) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Esto está vacío.<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$movies</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">xpath</span><span style="color: #007700">(</span><span style="color: #DD0000">"//movie"</span><span style="color: #007700">)) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
        </div>

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