gansub
gansub

Reputation: 1174

Parsing XML response with repeated tags

This is my XML response -

<?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>

I want to be able to get ALL the names of "Map" attribute i.e. time,isobaric,lat,lon. Currently with the below code it only prints out the first one. I tried iterating but it only prints the time variable n times.

 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'))

Upvotes: 0

Views: 828

Answers (1)

asongtoruin
asongtoruin

Reputation: 10359

Two small changes:

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

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

findall gets all matches, and when you are looping over child in map you want to be sure that you are returning values relating to child.

Upvotes: 2

Related Questions