久久久久久久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 對象)
主站蜘蛛池模板: 插插宗合网 | 久久黄网| 久久网一区二区 | 欧美精品久久久久久 | 本地毛片| 午夜视频在线视频 | 精品国产一区二区三区性色av | 成人精品久久日伦片大全免费 | 国产日韩一区二区 | 二区视频| 欧美午夜一区 | 亚洲一区免费 | 免费在线观看一区二区三区 | 凹凸日日摸日日碰夜夜 | 亚洲国产中文字幕 | 欧美日韩精品亚洲 | 伊人在线 | 网黄在线 | 成人一区二区三区 | 色婷婷综合久久久中文字幕 | 国产精品美女一区二区三区 | 在线观看国产视频 | 97视频在线观看网站 | 久久久久国产 | 午夜影院网站 | 亚洲欧美一区二区在线观看 | 大乳boobs巨大吃奶挤奶 | 亚洲精品视频在线看 | 美女黄视频网站 | 北条麻妃一区二区三区在线观看 | 亚洲成人免费视频在线 | 7777奇米影视 | 欧美精品91 | 老头搡老女人毛片视频在线看 | 色综合视频| 国产传媒在线观看 | 亚洲免费视频在线观看 | 国产精品av久久久久久久久久 | 欧美在线一区二区三区四区 | 在线视频一区二区 | 6996成人影院网在线播放 |