PHP解析WFS XML应答
我正在尝试解析从WFS请求到GeoServer的XML(或GML)回复 我的目标是将XML提取到一个漂亮整洁的数组中,然后显示、导出等等 我的XML如下所示:PHP解析WFS XML应答,xml,php,Xml,Php,我正在尝试解析从WFS请求到GeoServer的XML(或GML)回复 我的目标是将XML提取到一个漂亮整洁的数组中,然后显示、导出等等 我的XML如下所示: Array ( [0] => Array ( [id] => 27407951 [gridref] => SN123456 [species] => Milvus milvus [common] =>
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
)
27407951
SN123456
赤鸢
赤鸢
2004
鸡科
28064165
SN123456
水獭
欧亚水獭
09/11/2001
扭伤
鼬科
我希望最终结果如下所示:
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
)
)
等等
我正在使用下面的代码——这确实有效——但它似乎效率很低。对于XML中的每个记录,代码必须解析整个XML应答。如果只有20-30条记录,这是可以的,但是这个XML回复可能包含数千条记录
有没有一种方法可以只扫描一次XML文件就构建这个数组
以下是PHP:
//解析XML
$xml=simplexml\u load\u字符串($wfs\u 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\u members\u layer\u path=“//bis\u workspace:”$图层名称//即:“//bis_工作区:bis_1_优先物种”
foreach($xml->xpath($feature\u members\u layer\u path)作为$feature\u members\u raw)
{
$feature\u member\u id=$feature\u members\u raw->xpath('//bis\u workspace:id');
$feature_members_数组[$count]['id']=(字符串)$feature_members_id[$count];
$feature\u member\u gridref=$feature\u members\u raw->xpath('//bis\u workspace:gridref');
$feature\u members\u数组[$count]['gridref']=(字符串)$feature\u members\u gridref[$count];
$feature\u member\u species=$feature\u members\u raw->xpath('//bis\u workspace:species');
$feature_members_数组[$count]['species']=(字符串)$feature_members_species[$count];
$feature\u member\u common=$feature\u members\u raw->xpath('//bis\u workspace:common');
$feature\u members\u数组[$count]['common']=(字符串)$feature\u members\u common[$count];
$feature\u member\u date=$feature\u members\u raw->xpath('//bis\u workspace:date');
$feature\u members\u数组[$count]['date']=(字符串)$feature\u members\u date[$count];
$feature\u member\u comments=$feature\u members\u raw->xpath('//bis\u workspace:comments');
$feature_members_数组[$count]['comments']=(字符串)$feature_members_comments[$count];
$feature\u member\u family=$feature\u members\u raw->xpath('//bis\u workspace:family');
$feature\u members\u数组[$count]['family']=(字符串)$feature\u members\u family[$count];
$count++;
}
非常感谢,,
Steve我也一直在努力处理WFS XML中的名称空间。我的解决方案是切换到JSON
如果您的WFS支持,您可以将
outputFormat=json
添加到WFS请求中,事情会变得简单得多:)我也需要解决这个问题,而我的源代码没有json响应。诀窍在于使用$xml->getNamespaces(true)
并将其用作检索子级的参数
这项工作:
$xml='1!'
27407951
SN123456
赤鸢
赤鸢
2004
鸡科
28064165
SN123456
水獭
欧亚水獭
09/11/2001
扭伤
鼬科
';
$xml=simplexml\u load\u字符串($xml);
$ns=$xml->getnamespace(true);
$species_list=array();
foreach($xml->children($ns['gml'])->featureMembers->children($ns['bis_workspace'])->bis_1_priority_species作为$species_数据){
$species=array();
foreach($key=>value){
$species[$key]=(字符串)$value;
}
$species_list[]=$species;
}
回声“;
变量输出(物种列表);
回声“;
另一种解决方案(可能对某人有帮助):
gml文件:
$xmlFile = 'daten2.gml';
//Parse the XML
$xml = @simplexml_load_file($xmlFile);
$xpath = $xml->xpath( '//gml:PolygonPatch' );
$ezs_attr = (string) $xpath[0]->attr;
echo "The value0: $ezs_attr";
$xpath2 = $xml->xpath( '//gml:Curve' );
$xpath3 = $xml->xpath( '//gml:posList' );
$xpath4 = $xml->xpath( '//gml:FeatureCollection' );
$curve_ezs_prio = (string) $xpath2[0]->prio;
$curve_ezs_attr = (string) $xpath2[0]->attr;
echo "<br>";
echo "The value1: $curve_ezs_prio";
echo "<br>";
echo "The value2: $curve_ezs_attr";
echo "<br>";
echo "The value3: ".$xpath3[0];
echo "<br>";
echo "The value4: ".$xpath4[0];
echo "<br>";
echo "The value5: ".$xpath4[0]->reference->attributes()->key;
谢谢卢巴,这正是我最后所做的。PHPs对JSON的支持非常好,我很快就对数组进行了排序。有趣的是,回复看起来小了一点,所以节省了一点带宽。
The value0: 4220
The value1: 200
The value2: 8001
The value3: 412575.990 5791993.100 412595.334 5791979.677 412590.991 5791956.384 412574.411 5791969.017 412575.990 5791993.100
The value4:
The value5: BA2011_00001_1111