Python 3x:使用python xml.etree解析具有名称空间的xml文件

尼拉尼草

我正在尝试使用解析大型xml文件xml.etree它具有以下结构。

在此处输入图片说明

我对提取带有标题发布者的引用特别感兴趣,如下图所示。

在此处输入图片说明

以下是我尝试过的代码示例。它不打印任何内容。任何帮助表示赞赏。

import xml.etree.ElementTree as et

data = """<exist:result xmlns:exist="http://exist.sourceforge.net/NS/exist" exist:hits="1" exist:start="1" exist:count="1" exist:compilation-time="0" exist:execution-time="0">
    <events>
        <paging page="9" pageNumberOfRecords="20" totalNumberOfRecords="215"/>
        <WeatherEvent xmlns="http://hwe.niwa.co.nz/schema/2011" xmlns:gml="http://www.opengis.net/gml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://hwe.niwa.co.nz/schema/2011 ../hwe.xsd">
    <Identifier>November_2019_Timaru_Hail</Identifier>
    <Title> November 2019 Timaru Hail</Title>
    <StartDate>2019-11-20</StartDate> 
        <Abstract>A severe hailstorm over Timaru, with golf ball-sized hail stones, caused extensive damage to buildings and vehicles.</Abstract>
        <Notes/>
        <Regions>
            <Region name="Canterbury">
                <Hazards>
                    <Hazard type="Hail">
                        <Location name="Timaru">
                            <gml:Point gml:id="Timaru_1" srsName="urn:ogc:def:crs:EPSG:6.6:4326" srsDimension="2">
                                <gml:pos>-44.398445 171.255200</gml:pos>
                            </gml:Point>
                        </Location>
                        <Impacts>
                            <Impact type="InsuranceClaim" unit="$" value="130700000">Insurance claims totalled $130.7 million.</Impact>
                            
                            <Impact type="GeneralComment">Large hail stones smashed windows, pelted holes in roofs, damaged vehicles and forced the closure of businesses.</Impact>
                            <Impact type="GeneralComment">The Fire and Emergency NZ Mid-South Canterbury area commander said they had received 30 call-outs between noon and 2.40pm. Twenty one  of them were for hail or rain damage.</Impact>
                            <Impact type="GeneralComment">The South Canterbury Chamber of Commerce said there had been considerable damage and flooding with a number of businesses forced to close until their premises were secure and safe to open.  The Timaru library and the Aigantighe Art Gallery were both closed due to damage sustained.</Impact>
                            <Impact type="GeneralComment">A Timaru panel beating business estimated there were at least 10,000 vehicles in Timaru that were damaged by the hail.  Vehicles had dents, broken windscreens and broken wing mirrors.  The structural integrity of many of the damaged vehicles was found to be compromised.</Impact>
                            <Impact type="GeneralComment">An Australian-based team of hail damage repairers set up a base in Timaru to fix cars damaged in the hailstorm.  They anticipated that repairing hail-damaged cars in Timaru would take at least six months.</Impact>
                        </Impacts>
                    </Hazard>
                    <Hazard type="Hail">
                        <Location name="St Andrews">
                            <gml:Point gml:id="St_Andrews_1" srsName="urn:ogc:def:crs:EPSG:6.6:4326" srsDimension="2">
                                <gml:pos>-44.5301 171.1909</gml:pos>
                            </gml:Point>
                        </Location>
                        <Impacts>
                            <Impact type="GeneralComment">Federated Farmers reported there had been significant crop damage near St Andrews.</Impact>
                        </Impacts>
                    </Hazard>
                </Hazards>
            </Region>
        </Regions>
        <References>
            <Reference>
                <Title>Insurance Council of New Zealand (https://www.icnz.org.nz/natural-disasters/cost-of-natural-disasters/)</Title>
                <Type>Reference</Type>
            </Reference>            
            <Reference>
                <Title>Headline:  Giant hail stones hammer Timaru as storm moves up the country.</Title>
                <Type>Reference</Type>
                <Publisher>www.stuff.co.nz, 20 November 2019.  </Publisher>
            </Reference>
            <Reference>
                <Title>Headline:  Insurance companies face deluge of hail damage claims.</Title>
                <Type>Reference</Type>
                <Publisher>www.stuff.co.nz, 21 November 2019.  </Publisher>
            </Reference>
            <Reference>
                <Title>Headline:  Cars damaged in severe Timaru hailstorm failing warrents of fitness.</Title>
                <Type>Reference</Type>
                <Publisher>www.stuff.co.nz, 4 December 2019.  </Publisher>
            </Reference>
            <Reference>
                <Title>Headline:  Record insurance repairs for cars smashed by hail in Timaru.</Title>
                <Type>Reference</Type>
                <Publisher>www.stuff.co.nz, 23 December 2019.  </Publisher>
            </Reference>
            
        </References>
</WeatherEvent>
</events>
</exist:result>
"""

root = et.fromstring(data)

ns = {'exist':'http://exist.sourceforge.net/NS/exist', 'niwa':'http://hwe.niwa.co.nz/schema/2011'}


results = root.findall('exist:result', ns)
for event in results:
    weatherEvnt = event.find('niwa: events', ns)
    for WE in weatherEvnt:
        Ref = WE.find('niwa: WeatherEvent', ns)
        for x in Ref.find('niwa: References', ns):
            print(x.text)
克休斯

问题至少包括:

  1. 根已经是exist:result,所以初始

    results = root.findall('exist:result', ns)
    

    返回一个空列表,因为exist:result没有这样的子级。

  2. 冒号后的名称空间前缀及其本地名称后不应有空格。例如niwa: eventsniwa:events等。

  3. 没有的文字孩子niwa:References

不确定确切的最终目标是什么,但是这段代码

import xml.etree.ElementTree as et

data = "" # As specified in question.

root = et.fromstring(data)
ns = {'exist':'http://exist.sourceforge.net/NS/exist',
      'niwa':'http://hwe.niwa.co.nz/schema/2011'}

for ref in root.findall('.//niwa:Title', ns):
  print('Title='+ref.text)

将演示成功选择命名空间XML中的文本,并输出:

Title= November 2019 Timaru Hail
Title=Insurance Council of New Zealand (https://www.icnz.org.nz/natural-disasters/cost-of-natural-disasters/)
Title=Headline:  Giant hail stones hammer Timaru as storm moves up the country.
Title=Headline:  Insurance companies face deluge of hail damage claims.
Title=Headline:  Cars damaged in severe Timaru hailstorm failing warrents of fitness.
Title=Headline:  Record insurance repairs for cars smashed by hail in Timaru.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用Python lxml解析具有名称空间的XML文档

来自分类Dev

在Python中使用ElementTree解析具有名称空间的XML

来自分类Dev

使用j查询解析具有名称空间的xml文件

来自分类Dev

使用XmlDocument从具有或不具有名称空间的xml文件读取

来自分类Dev

解析具有名称空间的 XML 文件

来自分类Dev

如何使用python获取具有默认名称空间的xml文件中所有元素的xpath?

来自分类Dev

如何使用python获取具有默认名称空间的xml文件中所有元素的xpath?

来自分类Dev

使用Python xml.etree解析xml文件:空结果

来自分类Dev

使用xmlstarlet 1.6.1更新具有名称空间的XML

来自分类Dev

使用xmlstarlet 1.6.1更新具有名称空间的XML

来自分类Dev

如何使用JQuery解析具有名称空间的xml(并适用于所有浏览器..)?

来自分类Dev

使用JQuery解析具有名称空间的xml并适用于所有浏览器..

来自分类Dev

如何使用JQuery解析具有名称空间的xml(并适用于所有浏览器..)?

来自分类Dev

带有名称空间的python进程xml字符串

来自分类Dev

使用Powershell在具有名称空间的XML中选择具有Xpath的属性

来自分类Dev

使用Powershell在具有名称空间的XML中选择具有Xpath的属性

来自分类Dev

使用 xmldom 或 etree 使用 python 解析 XML

来自分类Dev

在Python中使用etree解析XML

来自分类Dev

解析带有名称空间的xml文件时出错

来自分类Dev

使用Python和xml.etree解析XML

来自分类Dev

使用Python和xml.etree解析XML

来自分类Dev

在python中使用xml.etree解析XML抛出TypeError

来自分类Dev

使用groovy删除具有名称空间的特定xml节点

来自分类Dev

使用Powershell将具有名称空间的XML转换为CSV

来自分类Dev

如何在具有名称空间的XML文档上使用LibreOffice FILTERXML函数?

来自分类Dev

XML文件解析Python

来自分类Dev

尝试使用Python 3解析XML文件

来自分类Dev

当XML上存在默认名称空间时,如何使用XSLT添加具有名称空间的属性

来自分类Dev

Python解析XML具有多个根

Related 相关文章

  1. 1

    使用Python lxml解析具有名称空间的XML文档

  2. 2

    在Python中使用ElementTree解析具有名称空间的XML

  3. 3

    使用j查询解析具有名称空间的xml文件

  4. 4

    使用XmlDocument从具有或不具有名称空间的xml文件读取

  5. 5

    解析具有名称空间的 XML 文件

  6. 6

    如何使用python获取具有默认名称空间的xml文件中所有元素的xpath?

  7. 7

    如何使用python获取具有默认名称空间的xml文件中所有元素的xpath?

  8. 8

    使用Python xml.etree解析xml文件:空结果

  9. 9

    使用xmlstarlet 1.6.1更新具有名称空间的XML

  10. 10

    使用xmlstarlet 1.6.1更新具有名称空间的XML

  11. 11

    如何使用JQuery解析具有名称空间的xml(并适用于所有浏览器..)?

  12. 12

    使用JQuery解析具有名称空间的xml并适用于所有浏览器..

  13. 13

    如何使用JQuery解析具有名称空间的xml(并适用于所有浏览器..)?

  14. 14

    带有名称空间的python进程xml字符串

  15. 15

    使用Powershell在具有名称空间的XML中选择具有Xpath的属性

  16. 16

    使用Powershell在具有名称空间的XML中选择具有Xpath的属性

  17. 17

    使用 xmldom 或 etree 使用 python 解析 XML

  18. 18

    在Python中使用etree解析XML

  19. 19

    解析带有名称空间的xml文件时出错

  20. 20

    使用Python和xml.etree解析XML

  21. 21

    使用Python和xml.etree解析XML

  22. 22

    在python中使用xml.etree解析XML抛出TypeError

  23. 23

    使用groovy删除具有名称空间的特定xml节点

  24. 24

    使用Powershell将具有名称空间的XML转换为CSV

  25. 25

    如何在具有名称空间的XML文档上使用LibreOffice FILTERXML函数?

  26. 26

    XML文件解析Python

  27. 27

    尝试使用Python 3解析XML文件

  28. 28

    当XML上存在默认名称空间时,如何使用XSLT添加具有名称空间的属性

  29. 29

    Python解析XML具有多个根

热门标签

归档