使用重复标记解析XML响应

时间:2016-10-07 15:55:07

标签: python xml xml-parsing httprequest python-3.5

这是我的XML响应 -

<?xml version="1.0" encoding="UTF-8"?>
<Dataset name="aggregations/g/ds083.2/2/TP"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xml.opendap.org/ns/DAP2"
xsi:schemaLocation="http://xml.opendap.org/ns/DAP2  http://xml.opendap.org/dap/dap2.xsd" >

<Attribute name="NC_GLOBAL" type="Container">
    <Attribute name="Originating_or_generating_Center" type="String">
        <value>US National Weather Service, National Centres for Environmental Prediction (NCEP)</value>
    </Attribute>
    <Attribute name="Originating_or_generating_Subcenter" type="String">
        <value>0</value>
    </Attribute>
    <Attribute name="GRIB_table_version" type="String">
        <value>2,1</value>
    </Attribute>
    <Attribute name="Type_of_generating_process" type="String">
        <value>Forecast</value>
    </Attribute>
    <Attribute name="Analysis_or_forecast_generating_process_identifier_defined_by_originating_centre" type="String">
        <value>Analysis from GDAS (Global Data Assimilation System)</value>
    </Attribute>
    <Attribute name="file_format" type="String">
        <value>GRIB-2</value>
    </Attribute>
    <Attribute name="Conventions" type="String">
        <value>CF-1.6</value>
    </Attribute>
    <Attribute name="history" type="String">
        <value>Read using CDM IOSP GribCollection v3</value>
    </Attribute>
    <Attribute name="featureType" type="String">
        <value>GRID</value>
    </Attribute>
    <Attribute name="_CoordSysBuilder" type="String">
        <value>ucar.nc2.dataset.conv.CF1Convention</value>
    </Attribute>
</Attribute>

<Grid  name="Temperature_isobaric">
    <Attribute name="long_name" type="String">
        <value>Temperature @ Isobaric surface</value>
    </Attribute>
    <Attribute name="units" type="String">
        <value>K</value>
    </Attribute>
    <Attribute name="abbreviation" type="String">
        <value>TMP</value>
    </Attribute>
    <Attribute name="missing_value" type="Float32">
        <value>NaN</value>
    </Attribute>
    <Attribute name="grid_mapping" type="String">
        <value>LatLon_Projection</value>
    </Attribute>
    <Attribute name="coordinates" type="String">
        <value>reftime3 time3 isobaric3 lat lon </value>
    </Attribute>
    <Attribute name="Grib_Variable_Id" type="String">
        <value>VAR_7-0--1-0_L100</value>
    </Attribute>
    <Attribute name="Grib2_Parameter" type="Int32">
        <value>0</value>
        <value>0</value>
        <value>0</value>
    </Attribute>
    <Attribute name="Grib2_Parameter_Discipline" type="String">
        <value>Meteorological products</value>
    </Attribute>
    <Attribute name="Grib2_Parameter_Category" type="String">
        <value>Temperature</value>
    </Attribute>
    <Attribute name="Grib2_Parameter_Name" type="String">
        <value>Temperature</value>
    </Attribute>
    <Attribute name="Grib2_Level_Type" type="String">
        <value>Isobaric surface</value>
    </Attribute>
    <Attribute name="Grib2_Generating_Process_Type" type="String">
        <value>Forecast</value>
    </Attribute>
    <Array name="Temperature_isobaric">
        <Float32/>
        <dimension name="time3" size="12911"/>
        <dimension name="isobaric3" size="31"/>
        <dimension name="lat" size="181"/>
        <dimension name="lon" size="360"/>
    </Array>
    <Map name="time3">
        <Float64/>
        <dimension name="time3" size="12911"/>
    </Map>
    <Map name="isobaric3">
        <Float32/>
        <dimension name="isobaric3" size="31"/>
    </Map>
    <Map name="lat">
        <Float32/>
        <dimension name="lat" size="181"/>
    </Map>
    <Map name="lon">
        <Float32/>
        <dimension name="lon" size="360"/>
    </Map>
</Grid>

我希望能够获得 ALL “Map”属性的名称,即time,isobaric,lat,lon。目前使用以下代码,打印出第一个。我尝试迭代但它只打印时间变量n次。

 import requests

 from xml.etree import ElementTree

 response = requests.get("http://rda.ucar.edu/thredds/dodsC/aggregations/g/ds083.2/2/TP.ddx?Temperature_isobaric")

 tree = ElementTree.fromstring(response.content)

 grid = tree.find("{http://xml.opendap.org/ns/DAP2}Grid")

 map = grid.find("{http://xml.opendap.org/ns/DAP2}Map")

for child in map:
  print(child.get('name'))

1 个答案:

答案 0 :(得分:2)

两个小变化:

map = grid.findall("{http://xml.opendap.org/ns/DAP2}Map")

for child in map:
  print(child.get('name'))

findall获取所有匹配项,当您循环child in map时,您希望确保返回与child相关的值。