我有一个XML文件:解析XML的Python:多相同的属性
<movie title="Enemy Behind">
<type>War, Thriller</type>
<type>WW2</type>
<format>DVD</format>
<year>2003</year>
<rating>PG</rating>
<stars>10</stars>
<description>Talk about a US-Japan war</description>
</movie>
,我使用下面的代码来解析这个XML在Python:
Print detail of each movie.
for movie in movies:
print ("*****Movie*****")
if movie.hasAttribute("title"):
print ("Title: %s" % movie.getAttribute("title"))
type = movie.getElementsByTagName('type')[0]
print ("Type: %s" % type.childNodes[0].data)
format = movie.getElementsByTagName('format')[0]
print ("Format: %s" % format.childNodes[0].data)
rating = movie.getElementsByTagName('rating')[0]
print ("Rating: %s" % rating.childNodes[0].data)
description = movie.getElementsByTagName('description')[0]
print ("Description: %s" % description.childNodes[0].data)
但仅使用此代码其中一个属性被打印,即“战争,惊悚片”。 “WW2”的另一个属性没有打印。
我应该使用for循环吗?我已经尝试过,并且出现错误“'元素'对象不可迭代”。
这说明了什么?打印类型(movie.getElementsByTagName('type')) – Bemmu