首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用重复标记解析XML响应

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

Stack Overflow用户
提问于 2016-10-07 15:55:07
回答 1查看 807关注 0票数 0

这是我的XML响应-

代码语言:javascript
复制
<?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>

我想得到所有的“地图”属性的名称,即时间,等压线,拉特,隆。目前,使用下面的代码,它只打印出第一个。我尝试了迭代,但它只打印了时间变量n次。

代码语言:javascript
复制
 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'))
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-07 16:12:40

两个小变化:

代码语言:javascript
复制
map = grid.findall("{http://xml.opendap.org/ns/DAP2}Map")

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

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39921353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档