title: PHP Parsing WFS XML Reply
body: <p>I'm trying to parse an XML (or GML) reply from a WFS request to GeoServer.</p>
  
  <p>My goal is to extract the XML into a nice neat array which I can then display, export, etc etc.</p>
  
  <p>My XML looks like this:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;wfs:FeatureCollection numberOfFeatures="52" timeStamp="2012-06-28T10:11:02.193Z" xsi:schemaLocation="http://squirrel.bis.local/bis_workspace http://squirrel.bis.local:8080/geoserver/wfs?service=WFS&amp;amp;version=1.1.0&amp;amp;request=DescribeFeatureType&amp;amp;typeName=bis_workspace%3Abis_1_priority_species http://www.opengis.net/wfs http://squirrel.bis.local:8080/geoserver/schemas/wfs/1.1.0/wfs.xsd" xmlns:opengeo="http://opengeo.org" xmlns:ogc="http://www.opengis.net/ogc" xmlns:bis_workspace="http://squirrel.bis.local/bis_workspace" xmlns:world="http://world.opengeo.org" xmlns:wfs="http://www.opengis.net/wfs" xmlns:medford="http://medford.opengeo.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ows="http://www.opengis.net/ows" xmlns:gml="http://www.opengis.net/gml" xmlns:usa="http://usa.opengeo.org" xmlns:xlink="http://www.w3.org/1999/xlink"&gt;
  &lt;gml:featureMembers&gt;
  &lt;bis_workspace:bis_1_priority_species gml:id="bis_1_priority_species.fid--2f086452_138094a10a0_116d"&gt;
  
  &lt;bis_workspace:id&gt;27407951&lt;/bis_workspace:id&gt;
  &lt;bis_workspace:gridref&gt;SN123456&lt;/bis_workspace:gridref&gt;
  &lt;bis_workspace:species&gt;Milvus milvus&lt;/bis_workspace:species&gt;
  &lt;bis_workspace:common&gt;Red Kite&lt;/bis_workspace:common&gt;
  &lt;bis_workspace:date&gt;2004&lt;/bis_workspace:date&gt;
  &lt;bis_workspace:comments&gt;
  &lt;/bis_workspace:comments&gt;
  &lt;bis_workspace:family&gt;Accipitridae&lt;/bis_workspace:family&gt;
  &lt;/bis_workspace:bis_1_priority_species&gt;
  
  &lt;bis_workspace:bis_1_priority_species gml:id="bis_1_priority_species.fid--2f086452_138094a10a0_116e"&gt;
  &lt;bis_workspace:id&gt;28064165&lt;/bis_workspace:id&gt;
  &lt;bis_workspace:gridref&gt;SN123456&lt;/bis_workspace:gridref&gt;
  &lt;bis_workspace:species&gt;Lutra lutra&lt;/bis_workspace:species&gt;
  &lt;bis_workspace:common&gt;European Otter&lt;/bis_workspace:common&gt;
  &lt;bis_workspace:date&gt;09/11/2001&lt;/bis_workspace:date&gt;
  &lt;bis_workspace:comments&gt;spraint&lt;/bis_workspace:comments&gt;
  &lt;bis_workspace:family&gt;Mustelidae&lt;/bis_workspace:family&gt;
  &lt;/bis_workspace:bis_1_priority_species&gt;
  
  &lt;/gml:featureMembers&gt;
  &lt;/wfs:FeatureCollection&gt;
  </code></pre>
  
  <p>I'd like the end result to look like this:</p>
  
  <pre><code>Array
  (
      [0] =&gt; Array
      (
              [id] =&gt; 27407951
              [gridref] =&gt; SN123456
              [species] =&gt; Milvus milvus
              [common] =&gt; Red Kite
              [date] =&gt; 2004
              [comments] =&gt; 
              [family] =&gt; Accipitridae
          )
  
  [1] =&gt; Array
      (
          [id] =&gt; 28064165
          [gridref] =&gt; SN123456
          [species] =&gt; Lutra lutra
          [common] =&gt; European Otter
          [date] =&gt; 09/11/2001
          [comments] =&gt; spraint
          [family] =&gt; Mustelidae
      )
  
  [2] =&gt; Array
      (
          [id] =&gt; 27516850
          [gridref] =&gt; SN123456
          [species] =&gt; Tyto alba
          [common] =&gt; Barn Owl
          [date] =&gt; 2004
          [comments] =&gt; Pair, nest box in tree, 2 chicks
          [family] =&gt; Tytonidae
      )
  </code></pre>
  
  <p>)</p>
  
  <p>etc etc</p>
  
  <p>I'm using the following code - that does work - but it seems stupidly inefficient. For every record in the XML, the code has to parse the entire XML reply. This is fine if there are just 20-30 records but this XML reply could contain thousands of records.</p>
  
  <p>Is there a way of building this array with only 1 sweep of the XML file?</p>
  
  <p>Here's the PHP:</p>
  
  <pre class="lang-php prettyprint-override"><code>    //Parse the XML
      $xml = simplexml_load_string($wfs_reply, NULL, NULL, "http://squirrel.bis.local/bis_workspace");
      $xml-&gt;registerXPathNamespace('wfs', 'http://www.opengis.net/wfs');
      $xml-&gt;registerXPathNamespace('gml', 'http://www.opengis.net/gml');
      $xml-&gt;registerXPathNamespace('bis_workspace', 'http://squirrel.bis.local/bis_workspace');
  
      $count = 0;
      $feature_members_array = array();
      $feature_members_layer_path = "//bis_workspace:" . $layer_name; //I.e. '//bis_workspace:bis_1_priority_species'
      foreach($xml-&gt;xpath($feature_members_layer_path) as $feature_members_raw)
      {
  
          $feature_member_id = $feature_members_raw-&gt;xpath('//bis_workspace:id');
              $feature_members_array[$count]['id'] = (string)$feature_member_id[$count];
          $feature_member_gridref = $feature_members_raw-&gt;xpath('//bis_workspace:gridref');
              $feature_members_array[$count]['gridref'] = (string)$feature_member_gridref[$count];
          $feature_member_species = $feature_members_raw-&gt;xpath('//bis_workspace:species');
              $feature_members_array[$count]['species'] = (string)$feature_member_species[$count];
          $feature_member_common = $feature_members_raw-&gt;xpath('//bis_workspace:common');
              $feature_members_array[$count]['common'] = (string)$feature_member_common[$count];
          $feature_member_date = $feature_members_raw-&gt;xpath('//bis_workspace:date');
              $feature_members_array[$count]['date'] = (string)$feature_member_date[$count];
          $feature_member_comments = $feature_members_raw-&gt;xpath('//bis_workspace:comments');
              $feature_members_array[$count]['comments'] = (string)$feature_member_comments[$count];
          $feature_member_family = $feature_members_raw-&gt;xpath('//bis_workspace:family');
              $feature_members_array[$count]['family'] = (string)$feature_member_family[$count];
  
          $count ++;
      }
  </code></pre>
  
  <p>Many thanks,
  Steve</p>