使用xml.etree.ElementTree解析XML

时间:2016-01-27 14:50:42

标签: python xml parsing

我试图解析一个传递给函数参数的简单XML块。然后我想在最后一个<cd>元素中返回标题,其中:&#39;仍然有蓝调&#39;。出于某种原因,我在执行此操作时遇到了问题(第一次解析XML)。这是我现在的功能,它基于我从xml.etree.ElementTree文档中读取的内容:

def get_last_title(xmlstr):
  xml = ET.fromstring(xmlstr)
  return xml.findall('cd')[-1:].findall('title').text

XML就在这里:

xml_doc ='''<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
    <cd>
        <title>Empire Burlesque</title>
        <artist sex="male">Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist sex="female">Bonnie Tyler</artist>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
    <cd>
        <title>Greatest Hits</title>
        <artist sex="female">Dolly Parton</artist>
        <country>USA</country>
        <company>RCA</company>
        <price>9.90</price>
        <year>1982</year>
    </cd>
    <cd>
        <title>Still got the blues</title>
        <artist sex="male">Gary Moore</artist>
        <country>UK</country>
        <company>Virgin records</company>
        <price>10.20</price>
        <year>1990</year>
    </cd>
</catalog>
'''

1 个答案:

答案 0 :(得分:1)

您正在尝试切片找到的元素列表,而不是通过-1索引获取最后一个元素,然后使用findtext()方法查找内部标题:

xml.findall('cd')[-1].findtext('title')

演示:

>>> import xml.etree.cElementTree as ET
>>> 
>>> xml_doc ='''<?xml version="1.0" encoding="ISO-8859-1"?>
    Your XML here
... '''
>>> 
>>> xml = ET.fromstring(xml_doc)
>>> print(xml.findall('cd')[-1].findtext('title'))
Still got the blues