使用ElementTree进行Python XML解析:如何查找具有相同名称的元素的值?

时间:2015-04-02 08:24:41

标签: python xml

免责声明:我不熟悉Python,XML和编程。代码(我从互联网上窃取)有效,但有一些问题,我似乎无法找到答案或包裹我的大脑......

我正在尝试解析grants.gov xml extract website中的XML文件,目的是删除不属于“无限制”资格类别的所有授权(在XML中标记为“EligibilityCategory”为“99”) 。)并输出一个新的xml文件。

我在下面的代码正确删除了不感兴趣的资金opps,但也删除了拥有多个EligibilityCategorys的资金opps,其中还包括“99”。我认为这是因为.find只抓取第一次出现的动作。我尝试使用.findall但无法解决问题。提前感谢您的帮助。

import xml.etree.ElementTree as etree
tree = etree.parse('sample.xml')
root = tree.getroot()

for FundingOppSynopsis in root.findall('FundingOppSynopsis'): 
    ID = int(FundingOppSynopsis.find('EligibilityCategory').text)
    if ID != 99:
        root.remove(FundingOppSynopsis)

tree.write("Output/output.xml", xml_declaration=True, encoding='UTF-8', method="xml")

示例(显着减少)XML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Grants SYSTEM "http://apply07.grants.gov/search/dtd/XMLExtract.dtd">
<Grants>
    <FundingOppSynopsis>
        <FundingOppNumber>USDA-RMA-RME-2008-03</FundingOppNumber>
        <ApplicationsDueDate>03242008</ApplicationsDueDate>
        <Office>Risk Management Agency</Office>
        <Agency>Department of Agriculture</Agency>
        <EligibilityCategory>25</EligibilityCategory>
    </FundingOppSynopsis>
    <FundingOppSynopsis>
        <FundingOppNumber>NPS-ARRAWHIS100315</FundingOppNumber>
        <ApplicationsDueDate>11282009</ApplicationsDueDate>
        <Office>National Park Service</Office>
        <Agency>Department of the Interior</Agency>
        <EligibilityCategory>00</EligibilityCategory>
    </FundingOppSynopsis>
    <FundingOppSynopsis>
        <FundingOppNumber>OFDA-FY08-002-APS</FundingOppNumber>
        <ApplicationsDueDate>10102008</ApplicationsDueDate>
        <Office>None</Office>
        <Agency>Agency for International Development</Agency>
        <EligibilityCategory>99</EligibilityCategory>
    </FundingOppSynopsis>
    <FundingOppSynopsis>
        <FundingOppNumber>AK-NOI08-0004</FundingOppNumber>
        <ApplicationsDueDate>07142008</ApplicationsDueDate>
        <Office>Bureau of Land Management</Office>
        <Agency>Department of the Interior</Agency>
        <EligibilityCategory>99</EligibilityCategory>
    </FundingOppSynopsis>
    <FundingOppSynopsis>
        <FundingOppNumber>RD-RBP-BIOMASS-2007-FULL</FundingOppNumber>
        <ApplicationsDueDate>11162007</ApplicationsDueDate>
        <Office>Business and Cooperative Programs</Office>
        <Agency>Department of Agriculture</Agency>
        <EligibilityCategory>06</EligibilityCategory>
        <EligibilityCategory>12</EligibilityCategory>
        <EligibilityCategory>13</EligibilityCategory>
        <EligibilityCategory>20</EligibilityCategory>
        <EligibilityCategory>22</EligibilityCategory>
        <EligibilityCategory>23</EligibilityCategory>
        <EligibilityCategory>25</EligibilityCategory>
    </FundingOppSynopsis>
    <FundingOppSynopsis>
        <FundingOppNumber>BAA07-10</FundingOppNumber>
        <ApplicationsDueDateExplanation>The due dates and times established for the receipt of White Papers and Full Proposals are as indicated in Section IV, Paragraph 3 of the BAA. </ApplicationsDueDateExplanation>
        <Office>Office of Procurement Operations - Grants Division</Office>
        <Agency>Department of Homeland Security</Agency>
        <EligibilityCategory>00</EligibilityCategory>
        <EligibilityCategory>01</EligibilityCategory>
        <EligibilityCategory>02</EligibilityCategory>
        <EligibilityCategory>04</EligibilityCategory>
        <EligibilityCategory>05</EligibilityCategory>
        <EligibilityCategory>06</EligibilityCategory>
        <EligibilityCategory>07</EligibilityCategory>
        <EligibilityCategory>08</EligibilityCategory>
        <EligibilityCategory>11</EligibilityCategory>
        <EligibilityCategory>12</EligibilityCategory>
        <EligibilityCategory>13</EligibilityCategory>
        <EligibilityCategory>20</EligibilityCategory>
        <EligibilityCategory>21</EligibilityCategory>
        <EligibilityCategory>22</EligibilityCategory>
        <EligibilityCategory>23</EligibilityCategory>
        <EligibilityCategory>25</EligibilityCategory>
        <EligibilityCategory>99</EligibilityCategory>
    </FundingOppSynopsis>
</Grants>

2 个答案:

答案 0 :(得分:2)

您可以使用xPath请求来实现您想要的目标。

import xml.etree.ElementTree as etree
tree = etree.parse('sample.xml')
root = tree.getroot()

req = tree.findall("./FundingOppSynopsis[EligibilityCategory='99']")

for r in req:
    print r

我提出的请求返回了一个文件的所有FundingOppSynopsis元素的列表,这些元素的子标记为EligibilityCategory,其中包含文本&#39; 99&#39;。

有关xPath request here的更多信息。

有关Python here中xPath用法的更多信息。

答案 1 :(得分:1)

您需要使用findall提取类别列表,然后检查99是否在该列表中。你可以使用这样的list comprehension

for FundingOppSynopsis in root.findall('FundingOppSynopsis'): 
    IDs = [int(category.text) for category in FundingOppSynopsis.findall('EligibilityCategory')]
    if 99 not in IDs:
        root.remove(FundingOppSynopsis)