首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法解析XML响应并获取元素

无法解析XML响应并获取元素
EN

Stack Overflow用户
提问于 2016-10-07 08:45:25
回答 2查看 753关注 0票数 1

这是来自http请求的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>

    <Array name="time1">
        <Attribute name="units" type="String">
            <value>Hour since 2007-12-06T12:00:00Z</value>
        </Attribute>
        <Attribute name="standard_name" type="String">
            <value>time</value>
        </Attribute>
        <Attribute name="long_name" type="String">
            <value>GRIB forecast or observation time</value>
        </Attribute>
        <Attribute name="calendar" type="String">
            <value>proleptic_gregorian</value>
        </Attribute>
        <Attribute name="_CoordinateAxisType" type="String">
            <value>Time</value>
        </Attribute>
        <Float64/>
        <dimension name="time1" size="10380"/>
    </Array>

</Dataset>

我正在尝试使用Python3.5解析这个XML内容

代码语言:javascript
复制
from xml.etree import ElementTree

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

tree = ElementTree.fromstring(response.content)

attr = tree.find("Attribute")
print(attr)

当我打印这个时,我会得到一个None。我做错了什么?我还想访问"Array“标记,但这也会返回None

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-10-07 09:21:27

XML文档使用名称空间,因此您需要在代码中支持这一点。在文档中有一个解释和示例代码。

基本上你可以这样做:

代码语言:javascript
复制
import requests
from xml.etree import ElementTree

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

tree = ElementTree.fromstring(response.content)

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

>>> print(attr)
<Element '{http://xml.opendap.org/ns/DAP2}Attribute' at 0x7f147a292458>

# or declare the namespace like this
ns = {'dap2': 'http://xml.opendap.org/ns/DAP2'}
attr = tree.find("dap2:Attribute", ns)

>>> print(attr)
<Element '{http://xml.opendap.org/ns/DAP2}Attribute' at 0x7f147a292458>
票数 1
EN

Stack Overflow用户

发布于 2016-10-07 09:21:06

正如医生中所述,由于Dataset根标记的xmlns="http://xml.opendap.org/ns/DAP2"属性,您要查找的所有标记名称都必须以{http://xml.opendap.org/ns/DAP2}作为前缀。

代码语言:javascript
复制
# should find something
tree.find("{http://xml.opendap.org/ns/DAP2}Attribute")

阅读ElementTree文档的这一节还将向您展示如何使用名称空间字典来提高某些内容的可读性。

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

https://stackoverflow.com/questions/39913114

复制
相关文章

相似问题

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