Python 使用重复标记解析XML响应

Python 使用重复标记解析XML响应,python,xml,xml-parsing,httprequest,python-3.5,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 ht

这是我的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>
两个小变化:

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

for child in map:
  print(child.get('name'))
findall
获取所有匹配项,当您在map中循环
子对象时,您需要确保返回与
子对象相关的值

两个小更改:

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

for child in map:
  print(child.get('name'))
findall
获取所有匹配项,当您在map
中循环
子对象时,您需要确保返回的值与
子对象相关