如何使用来自 xml 的 xpath 访问其他标签深处的标签?
How to access a tag deep inside other tags using xpath from an xml?
我有深层嵌套的 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")
[]
但我一无所获。
我不确定这是否是您要查找的内容,但试试这个:
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.'
我有深层嵌套的 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")
[]
但我一无所获。
我不确定这是否是您要查找的内容,但试试这个:
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.'