<?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 => 'de',
  ),
  'this' => 
  array (
    0 => 'simplexml.examples-basic.php',
    1 => 'Grundlegende Verwendung von SimpleXML',
    2 => 'Grundlegende Verwendung von SimpleXML',
  ),
  'up' => 
  array (
    0 => 'simplexml.examples.php',
    1 => 'Beispiele',
  ),
  'prev' => 
  array (
    0 => 'simplexml.examples.php',
    1 => 'Beispiele',
  ),
  'next' => 
  array (
    0 => 'simplexml.examples-errors.php',
    1 => 'Behandlung von XML-Fehlern',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'de',
    '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">Grundlegende Verwendung von SimpleXML</h2>
  <p class="para">
   Viele Beispiele in dieser Referenz benötigen einen XML-String. Anstatt
   diesen String in jedem Beispiel zu wiederholen, legen wir ihn in einer
   Datei ab, die wir in den Beispielen inkludieren. Diese eingebundene Datei
   zeigen wir Ihnen im folgenden Beispiel. Alternativ dazu können Sie ein
   XML-Dokument erstellen und es mit <span class="function"><a href="function.simplexml-load-file.php" class="function">simplexml_load_file()</a></span>
   einlesen.
  </p>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Beispiel #1 Einzubindende Datei examples/simplexml-data.php mit XML-String</strong></p>
    <div class="example-contents">
<div class="annotation-interactive 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">
   Die Einfachheit von SimpleXML ist besonders deutlich erkennbar, wenn man
   einen String oder eine Zahl aus einem XML-Dokument extrahieren will.
   <div class="example" id="example-2">
    <p><strong>Beispiel #2 <code class="literal">&lt;plot&gt;</code> ermitteln</strong></p>
    <div class="example-contents">
<div class="annotation-interactive 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>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive 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">
   Um auf Elemente in einem XML-Dokument zuzugreifen, die Zeichen enthalten,
   die nicht dem PHP-Benennungsstandard entsprechen (z. B. der Bindestrich),
   können Sie den Elementnamen zwischen geschweifte Klammern und
   einfachen Anführungszeichen einschließen.
   <div class="example" id="example-3">
    <p><strong>Beispiel #3 Auf <code class="literal">&lt;line&gt;</code> zugreifen</strong></p>
    <div class="example-contents">
<div class="annotation-interactive 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>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
PHP solves all my web problems
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4">
    <p><strong>Beispiel #4 Auf nicht-eindeutige Elemente per SimpleXML zugreifen</strong></p>
    <div class="example-contents"><p>
     Existieren mehrere Instanzen eines Elements als Kinder eines einzigen
     Elternelements, lassen sich normale iterative Techniken einsetzen.
    </p></div>
    <div class="example-contents">
<div class="annotation-interactive 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">/* Für jeden &lt;character&gt;-Knoten geben wir ein separates &lt;name&gt; aus. */<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>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive 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">Hinweis</strong>: 
   <p class="para">
    Eigenschaften (<code class="literal">$movies-&gt;movie</code> im vorherigen Beispiel)
    sind keine Arrays. Sie sind
    <a href="class.iterator.php" class="link">iterierbare</a> und
    <a href="class.arrayaccess.php" class="link">zugreifbare</a> Objekte.
   </p>
  </p></blockquote>
  <p class="para">
   <div class="example" id="example-5">
    <p><strong>Beispiel #5 Attribute verwenden</strong></p>
    <div class="example-contents"><p>
     Bis jetzt haben wir nur das Auslesen von Elementnamen und ihrer Werte
     betrachtet. SimpleXML kann aber auch auf die Attribute eines Elements
     zugreifen. Der Zugriff auf die Attribute eines Elements funktioniert
     genauso, als würden Sie die Elemente eines <span class="type"><a href="language.types.array.php" class="link">Array</a></span>s
     ansprechen.
    </p></div>
    <div class="example-contents">
<div class="annotation-interactive 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">/* Zugriff auf die &lt;rating&gt;-Knoten des ersten Films<br /> * Die Bewertungsskala wird zusätzlich ausgegeben. */<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">// Verwende Attribute als Element-Indizes<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">' Daumen hoch'</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">' Sterne'</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>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
7 Daumen hoch5 Sterne
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-6">
    <p><strong>Beispiel #6 Vergleichen von Elementen und Attributen mit Text</strong></p>
    <div class="example-contents"><p>
     Um ein Element oder ein Attribut mit einem String zu vergleichen oder an
     eine Funktion zu übergeben, die einen String als Parameter benötigt,
     müssen Sie das Element respektive das Attribut in einen String casten.
     Verwenden Sie dafür <code class="literal">(string)</code>, ansonsten behandelt PHP
     das Element als Objekt.
    </p></div>
    <div class="example-contents">
<div class="annotation-interactive 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">'Mein Lieblingsfilm.'</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>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive 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>Beispiel #7 Zwei Elemente vergleichen</strong></p>
    <div class="example-contents"><p>
     Zwei SimpleXMLElements können auch dann als unterschiedlich betrachtet
     werden, wenn sie auf das gleiche Element zeigen.
    </p></div>
    <div class="example-contents">
<div class="annotation-interactive 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>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
bool(false)
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-8">
    <p><strong>Beispiel #8 XPath verwenden</strong></p>
    <div class="example-contents"><p>
     SimpleXML verfügt über eine eingebaute
     <abbr title="XML Path Language">XPath</abbr>-Unterstützung. So können alle
     <code class="literal">&lt;character&gt;</code>-Elemente gefunden werden.
    </p></div>
    <div class="example-contents"><p>
    &#039;<code class="literal">//</code>&#039; wird hier als Wildcard verwendet. Um absolute
     Pfadangaben zu verwenden, lassen Sie einen der Schrägstriche weg:
    </p></div>
    <div class="example-contents">
<div class="annotation-interactive 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">' gespielt von '</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>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
Ms. Coder gespielt von Onlivia Actora
Mr. Coder gespielt von El ActÓr
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-9">
    <p><strong>Beispiel #9 Werte setzen</strong></p>
    <div class="example-contents"><p>
     Die Daten in SimpleXML müssen nicht konstant sein. Das Objekt erlaubt
     die Veränderung all seiner Elemente.
    </p></div>
    <div class="example-contents">
<div class="annotation-interactive 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>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive 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>Beispiel #10 Elemente und Attribute hinzufügen</strong></p>
    <div class="example-contents"><p>
     SimpleXML verfügt über eine vereinfachte Möglichkeit, Kindelemente und
     Attribute hinzuzufügen.
    </p></div>
    <div class="example-contents">
<div class="annotation-interactive 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>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive 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>Beispiel #11 DOM-Interoperabilität</strong></p>
    <div class="example-contents"><p>
     PHP hat einen Mechanismus, um XML-Knoten zwischen dem SimpleXML und dem
     DOM-Format zu konvertieren. Das Beispiel zeigt, wie man ein DOM-Element
     in SimpleXML verwandelt.
    </p></div>
    <div class="example-contents">
<div class="annotation-interactive 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">'Fehler beim Parsen des Dokuments'</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>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
blah
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-12">
    <p><strong>Beispiel #12 Verwenden von Namensräumen</strong></p>
    <div class="example-contents">
<div class="annotation-interactive 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">// Der Namensraum http://www.w3.org/XML/1998/namespace ist als "xml" verfügbar.<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">// Auf Attribute mit Namensraum kann mit attributes() zugegriffen werden.<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">// Durch die Verwendung der Namensraum-URI kann das Dokument jeden beliebigen<br />// Namensraum-Alias verwenden.<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">// Auf Kinder kann mit children() zugegriffen werden.<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">// Um xpath() mit einem Namensraum zu benutzen, muss dieser zuerst registriert<br />// werden.<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">// Auch der Standard-Namensraum muss registriert werden.<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">// Dies ist leer.<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); ?>