title: Extracting Certain XML Elements with PHP SimpleXML
body: <p>I am having some problems parsing this piece of XML using SimpleXML. There is always only one Series element, and a variable number of Episode elements beneath. I want to parse XML so I can store the Series data in one table, and all the Episode data in another table.</p>
  
  <p><strong>XML:</strong></p>
  
  <pre><code>&lt;Data&gt;
      &lt;Series&gt;
          &lt;id&gt;80348&lt;/id&gt;
          &lt;Genre&gt;|Action and Adventure|Comedy|Drama|&lt;/Genre&gt;
          &lt;IMDB_ID&gt;tt0934814&lt;/IMDB_ID&gt;
          &lt;SeriesID&gt;68724&lt;/SeriesID&gt;
          &lt;SeriesName&gt;Chuck&lt;/SeriesName&gt;
          &lt;banner&gt;graphical/80348-g.jpg&lt;/banner&gt;
      &lt;/Series&gt;
      &lt;Episode&gt;
          &lt;id&gt;935481&lt;/id&gt;
          &lt;Director&gt;Robert Duncan McNeill&lt;/Director&gt;
          &lt;EpisodeName&gt;Chuck Versus the Third Dimension 2D&lt;/EpisodeName&gt;
          &lt;EpisodeNumber&gt;1&lt;/EpisodeNumber&gt;
          &lt;seasonid&gt;27984&lt;/seasonid&gt;
          &lt;seriesid&gt;80348&lt;/seriesid&gt;
      &lt;/Episode&gt;
      &lt;Episode&gt;
          &lt;id&gt;935483&lt;/id&gt;
          &lt;Director&gt;Robert Duncan McNeill&lt;/Director&gt;
          &lt;EpisodeName&gt;Buy More #15: Employee Health&lt;/EpisodeName&gt;
          &lt;EpisodeNumber&gt;2&lt;/EpisodeNumber&gt;
          &lt;seasonid&gt;27984&lt;/seasonid&gt;
          &lt;seriesid&gt;80348&lt;/seriesid&gt;
      &lt;/Episode&gt;
  &lt;/Data&gt;
  </code></pre>
  
  <p>When I attempt to access just the first Series element and child nodes, or iterate through the Episode elements only it does not work. I have also tried to use DOMDocument with SimpleXML, but could not get that to work at all.</p>
  
  <p><strong>PHP Code:</strong></p>
  
  <pre><code>&lt;?php
      if(file_exists('en.xml'))
      {
          $data = simplexml_load_file('en.xml');
          foreach($data as $series)
          {
              echo 'id: &lt;br /&gt;' . $series-&gt;id;
              echo 'imdb: &lt;br /&gt;' . $series-&gt;IMDB_ID;
          }
      }
  ?&gt;
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <pre><code>id:80348
  imdb:tt0934814
  id:935481
  imdb:
  id:1534641
  imdb:
  </code></pre>