如果我有类似的东西:
<?xml version='1.0' encoding='UTF-8'?>
<movies xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="moviescheme.xsd">
<movie>
<title>Million Dollar Baby</title>
<formattype>video</formattype>
<genre>Drama</genre>
<genre>Sport</genre>
<released>
2004
</released>
<rate>
<rottentomatoes>91%</rottentomatoes>
<imbd>8.1</imbd>
</rate>
</movie>
<movie>
<title>Room</title>
<formattype>video</formattype>
<genre>Drama</genre>
<released>
2015
</released>
<rate>
<rottentomatoes>94%</rottentomatoes>
<imbd>8.2</imbd>
</rate>
</movie>
</movies>
如何搜索单词“Room”,例如,获取相关电影标签的所有相关标签 - 并将其打印出来?
答案 0 :(得分:0)
您可以将lxml与xpath一起用于教程,请参阅http://lxml.de/tutorial.html
from lxml import etree
# Read the xml file.
root = etree.parse("movies.xml")
# get the movie element with the title test Room
elems = root.xpath("//movie[title/text()='Room']")
# for the child elments print the data recursing through other children
def printitems (elems):
for i in elems:
print("%s - %s" % (i.tag, i.text))
printitems(i)
printitems (elems[0])
给出
title - Room
formattype - video
genre - Drama
released -
2015
rate -
rottentomatoes - 94%
imbd - 8.2
编辑通过儿童的子节点递归。