如何从 GML 文件中提取数据

How to extract data from GML file

我有一个文本文件,想从中提取 <gml:pos>73664.300 836542.700</gml:pos>。更准确地说,我想从 pos 标签中获取 GPS 坐标系 [73664.300 836542.700]。该文件包含多个<wfs:member>,每个<gml:pos>(最深层)。

<?xml version='1.0' encoding='UTF-8'?>
<wfs:FeatureCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/wfs/2.0 http://schemas.opengis.net/wfs/2.0/wfs.xsd http://www.opengis.net/gml/3.2 http://schemas.opengis.net/gml/3.2.1/gml.xsd http://www.deegree.org/app https://web.de/feature_descr?SERVICE=WFS&amp;VERSION=2.0.0&amp;REQUEST=DescribeFeatureType&amp;OUTPUTFORMAT=application%2Fgml%2Bxml%3B+version%3D3.2&amp;TYPENAME=app:lsa_data&amp;NAMESPACES=xmlns(app,http%3A%2F%2Fwww.deegree.org%2Fapp)" xmlns:wfs="http://www.opengis.net/wfs/2.0" timeStamp="2020-11-18T15:01:17Z" xmlns:gml="http://www.opengis.net/gml/3.2" numberMatched="unknown" numberReturned="0">
  <!--NOTE: numberReturned attribute should be 'unknown' as well, but this would not validate against the current version of the WFS 2.0 schema (change upcoming). See change request (CR 144): https://portal.opengeospatial.org/files?fact_id=6798.-->
  <wfs:member>
    <app:dat_set xmlns:app="http://www.deegree.org/app" gml:id="app:dat_set_1">
      <app:point>2</app:point>
      <app:art>K         </app:art>
      <app:L_Name>westt / woustest             </app:L_Name>
      <app:geom>
        <!--Inlined geometry 'data_1_APP_GEOM'-->
        <gml:MultiPoint gml:id="data_1_APP_GEOM" srsName="EPSG:25832">
          <gml:pointMember>
            <gml:Point gml:id="GEOMETRY_ad608059-f297-4554-8464-cdde248cb531" srsName="EPSG:25832">
              <gml:pos>73664.300 836542.700</gml:pos>
            </gml:Point>
          </gml:pointMember>
        </gml:MultiPoint>
      </app:geom>
    </app:lsa_pointdata>
  </wfs:member>
  <wfs:member>
    <app:dat_set xmlns:app="http://www.deegree.org/app" gml:id="app:dat_set_2">
      <app:point>3</app:point>
      <app:art>K         </app:art>
      <app:L_Name>route / riztr        </app:L_Name>
      <app:geom>
        <!--Inlined geometry 'data_2_APP_GEOM'-->
        <gml:MultiPoint gml:id="data_2_APP_GEOM" srsName="EPSG:25832">
          <gml:pointMember>
            <gml:Point gml:id="GEOMETRY_440d8630-b674-4768-a5b7-3fab46d9ac8c" srsName="EPSG:25832">
              <gml:pos>74354.900 837456.300</gml:pos>
            </gml:Point>
          </gml:pointMember>
        </gml:MultiPoint>
      </app:geom>
    </app:lsa_pointdata>
  </wfs:member>
  <wfs:member>
    ...
...

我怎样才能得到那些 gps 坐标?
提前谢谢你。

您可以使用 lxml 和 XPATH。

data = b'''\
<?xml version='1.0' encoding='UTF-8'?>
<wfs:FeatureCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/wfs/2.0 http://schemas.opengis.net/wfs/2.0/wfs.xsd http://www.opengis.net/gml/3.2 http://schemas.opengis.net/gml/3.2.1/gml.xsd http://www.deegree.org/app https://web.de/feature_descr?SERVICE=WFS&amp;VERSION=2.0.0&amp;REQUEST=DescribeFeatureType&amp;OUTPUTFORMAT=application%2Fgml%2Bxml%3B+version%3D3.2&amp;TYPENAME=app:lsa_data&amp;NAMESPACES=xmlns(app,http%3A%2F%2Fwww.deegree.org%2Fapp)" xmlns:wfs="http://www.opengis.net/wfs/2.0" timeStamp="2020-11-18T15:01:17Z" xmlns:gml="http://www.opengis.net/gml/3.2" numberMatched="unknown" numberReturned="0">
  <!--NOTE: numberReturned attribute should be 'unknown' as well, but this would not validate against the current version of the WFS 2.0 schema (change upcoming). See change request (CR 144): https://portal.opengeospatial.org/files?fact_id=6798.-->
  <wfs:member>
    <app:dat_set xmlns:app="http://www.deegree.org/app" gml:id="app:dat_set_1">
      <app:point>2</app:point>
      <app:art>K         </app:art>
      <app:L_Name>westt / woustest             </app:L_Name>
      <app:geom>
        <!--Inlined geometry 'data_1_APP_GEOM'-->
        <gml:MultiPoint gml:id="data_1_APP_GEOM" srsName="EPSG:25832">
          <gml:pointMember>
            <gml:Point gml:id="GEOMETRY_ad608059-f297-4554-8464-cdde248cb531" srsName="EPSG:25832">
              <gml:pos>73664.300 836542.700</gml:pos>
            </gml:Point>
          </gml:pointMember>
        </gml:MultiPoint>
      </app:geom>
    </app:dat_set>
  </wfs:member>
  <wfs:member>
    <app:dat_set xmlns:app="http://www.deegree.org/app" gml:id="app:dat_set_2">
      <app:point>3</app:point>
      <app:art>K         </app:art>
      <app:L_Name>route / riztr        </app:L_Name>
      <app:geom>
        <!--Inlined geometry 'data_2_APP_GEOM'-->
        <gml:MultiPoint gml:id="data_2_APP_GEOM" srsName="EPSG:25832">
          <gml:pointMember>
            <gml:Point gml:id="GEOMETRY_440d8630-b674-4768-a5b7-3fab46d9ac8c" srsName="EPSG:25832">
              <gml:pos>74354.900 837456.300</gml:pos>
            </gml:Point>
          </gml:pointMember>
        </gml:MultiPoint>
      </app:geom>
    </app:dat_set>
  </wfs:member>
</wfs:FeatureCollection>
'''

from lxml import etree
from io import BytesIO
f = BytesIO(data)

ns = {"gml": "http://www.opengis.net/gml/3.2"}
tree = etree.parse(f)
for e in tree.findall("//gml:pos", ns):
    print(e.text)