PHP Parsing WFS XML Reply

iBeko

New Member
I'm trying to parse an XML (or GML) reply from a WFS request to GeoServer.My goal is to extract the XML into a nice neat array which I can then display, export, etc etc.My XML looks like this:\[code\]<?xml version="1.0" encoding="UTF-8"?><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&version=1.1.0&request=DescribeFeatureType&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"><gml:featureMembers><bis_workspace:bis_1_priority_species gml:id="bis_1_priority_species.fid--2f086452_138094a10a0_116d"><bis_workspace:id>27407951</bis_workspace:id><bis_workspace:gridref>SN123456</bis_workspace:gridref><bis_workspace:species>Milvus milvus</bis_workspace:species><bis_workspace:common>Red Kite</bis_workspace:common><bis_workspace:date>2004</bis_workspace:date><bis_workspace:comments></bis_workspace:comments><bis_workspace:family>Accipitridae</bis_workspace:family></bis_workspace:bis_1_priority_species><bis_workspace:bis_1_priority_species gml:id="bis_1_priority_species.fid--2f086452_138094a10a0_116e"><bis_workspace:id>28064165</bis_workspace:id><bis_workspace:gridref>SN123456</bis_workspace:gridref><bis_workspace:species>Lutra lutra</bis_workspace:species><bis_workspace:common>European Otter</bis_workspace:common><bis_workspace:date>09/11/2001</bis_workspace:date><bis_workspace:comments>spraint</bis_workspace:comments><bis_workspace:family>Mustelidae</bis_workspace:family></bis_workspace:bis_1_priority_species></gml:featureMembers></wfs:FeatureCollection>\[/code\]I'd like the end result to look like this:\[code\]Array( [0] => Array ( [id] => 27407951 [gridref] => SN123456 [species] => Milvus milvus [common] => Red Kite [date] => 2004 [comments] => [family] => Accipitridae )[1] => Array ( [id] => 28064165 [gridref] => SN123456 [species] => Lutra lutra [common] => European Otter [date] => 09/11/2001 [comments] => spraint [family] => Mustelidae )[2] => Array ( [id] => 27516850 [gridref] => SN123456 [species] => Tyto alba [common] => Barn Owl [date] => 2004 [comments] => Pair, nest box in tree, 2 chicks [family] => Tytonidae )\[/code\])etc etcI'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.Is there a way of building this array with only 1 sweep of the XML file?Here's the PHP:\[code\] //Parse the XML $xml = simplexml_load_string($wfs_reply, NULL, NULL, "http://squirrel.bis.local/bis_workspace"); $xml->registerXPathNamespace('wfs', 'http://www.opengis.net/wfs'); $xml->registerXPathNamespace('gml', 'http://www.opengis.net/gml'); $xml->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->xpath($feature_members_layer_path) as $feature_members_raw) { $feature_member_id = $feature_members_raw->xpath('//bis_workspace:id'); $feature_members_array[$count]['id'] = (string)$feature_member_id[$count]; $feature_member_gridref = $feature_members_raw->xpath('//bis_workspace:gridref'); $feature_members_array[$count]['gridref'] = (string)$feature_member_gridref[$count]; $feature_member_species = $feature_members_raw->xpath('//bis_workspace:species'); $feature_members_array[$count]['species'] = (string)$feature_member_species[$count]; $feature_member_common = $feature_members_raw->xpath('//bis_workspace:common'); $feature_members_array[$count]['common'] = (string)$feature_member_common[$count]; $feature_member_date = $feature_members_raw->xpath('//bis_workspace:date'); $feature_members_array[$count]['date'] = (string)$feature_member_date[$count]; $feature_member_comments = $feature_members_raw->xpath('//bis_workspace:comments'); $feature_members_array[$count]['comments'] = (string)$feature_member_comments[$count]; $feature_member_family = $feature_members_raw->xpath('//bis_workspace:family'); $feature_members_array[$count]['family'] = (string)$feature_member_family[$count]; $count ++; }\[/code\]Many thanks,Steve
 
Back
Top