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

訪問使用 ElementTree 解析的 xml 文件中的嵌套子項(xiàng)

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

問題描述

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

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

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

但是當(dāng)我使用 ElementTree 訪問它并查找 child 標(biāo)記和屬性時(shí),

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 {}

我認(rèn)為這意味著它們的屬性是空的.為什么會(huì)這樣,如何訪問嵌套在 EstablishmentDetailScores 中的子級(jí)?

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?

編輯

感謝下面的答案,我可以進(jìn)入樹內(nèi),但是如果我想檢索諸如 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 

并產(chǎn)生

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 中的所有標(biāo)簽,您需要找到該標(biāo)簽,然后遍歷其子標(biāo)簽!
    • 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 {}
      

      • 要獲得您在評(píng)論中提到的 Hygiene 的分?jǐn)?shù),
      • 您所做的是,它將獲得第一個(gè) Scores 標(biāo)簽,并且當(dāng)您在 root.find('.//Scores'):rating=child.get('Hygiene').也就是說,顯然所有三個(gè)孩子都不會(huì)有元素!

        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 標(biāo)簽.- 在找到的每個(gè)標(biāo)簽中找到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
        

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

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

相關(guān)文檔推薦

Troubles while parsing with python very large xml file(使用 python 解析非常大的 xml 文件時(shí)出現(xiàn)問題)
Find all nodes by attribute in XML using Python 2(使用 Python 2 在 XML 中按屬性查找所有節(jié)點(diǎn))
Python - How to parse xml response and store a elements value in a variable?(Python - 如何解析 xml 響應(yīng)并將元素值存儲(chǔ)在變量中?)
How to get XML tag value in Python(如何在 Python 中獲取 XML 標(biāo)記值)
How to correctly parse utf-8 xml with ElementTree?(如何使用 ElementTree 正確解析 utf-8 xml?)
Parse XML from URL into python object(將 XML 從 URL 解析為 python 對(duì)象)
主站蜘蛛池模板: 插少妇 | 欧美日韩黄色片 | 久久久久久免费 | 色综合天天综合网天天狠天天 | 一区二区国产在线 | 日本在线不卡视频 | 久久人人爽人人爽人人片 | 中文在线资源 | 韩日中文字幕 | 免费看黄视频网站 | 日韩女优在线观看 | 精品国产91 | 久久久综合视频 | 欧美一区二区三区在线观看 | 成人免费在线视频观看 | 91超碰在线播放 | 蜜桃视频一区 | 午夜免费视频 | 午夜精品久久久久久久久久蜜桃 | 成人在线a | 亚洲精品成人网 | 欧美二三区 | 精品一区在线播放 | 久草成人 | 四级黄色片 | 欧美日韩国产二区 | 精品日韩一区二区三区 | 午夜精品视频在线 | 一级黄色免费 | 天天色播 | 日韩精品欧美 | 国产91丝袜在线播放 | 免费看av的网址 | 四虎在线免费观看视频 | 久久嫩草 | 国产成人免费视频 | 天天操综合网 | 国产视频999| 一级片观看| 久久综合激情 | 涩涩久久 |