久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

訪問使用 ElementTree 解析的 xml 文件中的嵌套子項

Access nested children in xml file parsed with ElementTree(訪問使用 ElementTree 解析的 xml 文件中的嵌套子項)
本文介紹了訪問使用 ElementTree 解析的 xml 文件中的嵌套子項的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我是 xml 解析的新手.此 xml 文件 具有以下樹:

I am new to xml parsing. This xml file has the following tree:

FHRSEstablishment
 |--> Header
 |    |--> ...
 |--> EstablishmentCollection
 |    |--> EstablishmentDetail
 |    |    |-->...
 |    |--> Scores
 |    |    |-->...
 |--> EstablishmentCollection
 |    |--> EstablishmentDetail
 |    |    |-->...
 |    |--> Scores
 |    |    |-->...

但是當我使用 ElementTree 訪問它并查找 child 標記和屬性時,

but when I access it with ElementTree and look for the child tags and attributes,

import xml.etree.ElementTree as ET
import urllib2
tree = ET.parse(
   file=urllib2.urlopen('http://ratings.food.gov.uk/OpenDataFiles/FHRS408en-GB.xml' % i))
root = tree.getroot()
for child in root:
   print child.tag, child.attrib

我只得到:

Header {}
EstablishmentCollection {}

我認為這意味著它們的屬性是空的.為什么會這樣,如何訪問嵌套在 EstablishmentDetailScores 中的子級?

which I assume means that their attributes are empty. Why is it so, and how can I access the children nested inside EstablishmentDetail and Scores?

編輯

感謝下面的答案,我可以進入樹內,但是如果我想檢索諸如 Scores 中的值,這將失敗:

Thanks to the answers below I can get inside the tree, but if I want to retrieve values such as those in Scores, this fails:

for node in root.find('.//EstablishmentDetail/Scores'):
    rating = node.attrib.get('Hygiene')
    print rating 

并產生

None
None
None

這是為什么呢?

推薦答案

你必須在你的根目錄上迭代().

Yo have to iter() over your root.

那就是 root.iter() 可以解決問題!

that is root.iter() would do the trick!

import xml.etree.ElementTree as ET
import urllib2
tree =ET.parse(urllib2.urlopen('http://ratings.food.gov.uk/OpenDataFiles/FHRS408en-GB.xml'))
root = tree.getroot()
for child in root.iter():
   print child.tag, child.attrib

輸出:

FHRSEstablishment {}
Header {}
ExtractDate {}
ItemCount {}
ReturnCode {}
EstablishmentCollection {}
EstablishmentDetail {}
FHRSID {}
LocalAuthorityBusinessID {}
...

  • 要獲取 EstablishmentDetail 中的所有標簽,您需要找到該標簽,然后遍歷其子標簽!
    • To get all tags inside EstablishmentDetail you need to find that tag and then loop through its children!
    • 也就是說,例如.

      for child in root.find('.//EstablishmentDetail'):
          print child.tag, child.attrib
      

      輸出:

      FHRSID {}
      LocalAuthorityBusinessID {}
      BusinessName {}
      BusinessType {}
      BusinessTypeID {}
      RatingValue {}
      RatingKey {}
      RatingDate {}
      LocalAuthorityCode {}
      LocalAuthorityName {}
      LocalAuthorityWebSite {}
      LocalAuthorityEmailAddress {}
      Scores {}
      SchemeType {}
      NewRatingPending {}
      Geocode {}
      

      • 要獲得您在評論中提到的 Hygiene 的分數,
      • 您所做的是,它將獲得第一個 Scores 標簽,并且當您在 root.find('.//Scores'):rating=child.get('Hygiene').也就是說,顯然所有三個孩子都不會有元素!

        What you have done is, it will get the first Scores tag and that will have Hygiene, ConfidenceInManagement, Structural tags as child when you call for each in root.find('.//Scores'):rating=child.get('Hygiene'). That is, obviously all three child will not have the element!

        你需要先- 查找所有 Scores 標簽.- 在找到的每個標簽中找到Hygiene

        You need to first - find all Scores tag. - find Hygiene in every tags found!

        for each in root.findall('.//Scores'):
            rating = each.find('.//Hygiene')
            print '' if rating is None else rating.text
        

        輸出:

        5
        5
        5
        0
        5
        

        這篇關于訪問使用 ElementTree 解析的 xml 文件中的嵌套子項的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

        【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Troubles while parsing with python very large xml file(使用 python 解析非常大的 xml 文件時出現問題)
Find all nodes by attribute in XML using Python 2(使用 Python 2 在 XML 中按屬性查找所有節點)
Python - How to parse xml response and store a elements value in a variable?(Python - 如何解析 xml 響應并將元素值存儲在變量中?)
How to get XML tag value in Python(如何在 Python 中獲取 XML 標記值)
How to correctly parse utf-8 xml with ElementTree?(如何使用 ElementTree 正確解析 utf-8 xml?)
Parse XML from URL into python object(將 XML 從 URL 解析為 python 對象)
主站蜘蛛池模板: 欧美日韩久久精品 | 亚洲欧美在线观看视频 | 91成人小视频 | 国产人久久人人人人爽 | 精品一区二区在线观看 | 91精品久久久久久久久 | 久久亚洲国产精品 | 久久大| 日韩电影免费在线观看中文字幕 | 久久精品亚洲精品 | 久久精品久久综合 | www.日韩在线 | 99这里只有精品视频 | 视频一区在线播放 | 搞av.com| 在线中文字幕亚洲 | 亚洲国产成人精品女人久久久 | 日日夜夜免费精品视频 | 成人午夜精品一区二区三区 | 日韩欧美中文在线 | 国产视频中文字幕 | 久久国产高清 | 日韩在线视频免费观看 | av天天看 | 国产一区二区电影 | 天天搞天天操 | 国产成人99久久亚洲综合精品 | www.99热这里只有精品 | h视频在线免费看 | 日韩精品视频在线播放 | 青青草网| 中文字幕一区二区视频 | 亚洲免费婷婷 | 人人干视频在线 | 精品国产欧美一区二区三区成人 | 亚洲三区在线观看 | 欧美久久久久 | 91视频导航 | 亚洲成人免费视频 | 日韩在线不卡 | 精品成人一区二区 |