如何使用xml中的xpath访问其他标签内部的标签?

时间:2019-04-29 10:38:46

标签: python xpath lxml

我有一个深层的xml标记电影,我想使用xpath直接访问。

<?xml version='1.0' encoding='utf8'?>
<collection>
    <genre category="Action">
        <decade years="1980s">
            <movie favorite="True" title="Indiana Jones: The raiders of the lost Ark">
                <format multiple="No">DVD</format>
                <year>1981</year>
                <rating>PG</rating>
                <description>
                'Archaeologist and adventurer Indiana Jones 
                is hired by the U.S. government to find the Ark of the 
                Covenant before the Nazis.'
                </description>
            </movie>
</decade>
</genre>
</collection>
</xml>

如何访问movie标签及其属性? 我尝试使用

root  = etee.tostring(above_xml)
print root.xpath("movie")
[]

但是我什么也没得到。

1 个答案:

答案 0 :(得分:0)

我不确定这是您要找的东西,但是请尝试以下操作:

tree = lxml.etree.fromstring(xml_above, parser=lxml.etree.HTMLParser())
film =  tree.xpath("//movie/*/text()")
for i in film:
    print(i)

输出:

DVD
1981
PG

            'Archaeologist and adventurer Indiana Jones 
            is hired by the U.S. government to find the Ark of the 
            Covenant before the Nazis.'
相关问题