擅长:python、mysql、java
<p>您需要从XML以迭代方式构建<code>roms</code>列表:</p>
<pre><code>roms = []
for game in games:
rom_key = game.attributes['name']
roms.append(rom_key.value)
</code></pre>
<p>或者最好写成<a href="https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions" rel="nofollow">list-comprehension</a>:</p>
<pre><code>roms = [game.attributes['name'].value for game in games]
</code></pre>
<p>您还可以使用以下方法提取“任天堂娱乐系统”:</p>
<pre><code>xmldoc.getElementsByTagName('listname')[0].firstChild.data
</code></pre>
<p>这给我们留下了:</p>
<pre><code>from xml.dom import minidom
def databaseGameExtraction(xml):
xmldoc = minidom.parse(xml)
roms = [game.attributes['name'].value
for game in xmldoc.getElementsByTagName('game')]
compagny = xmldoc.getElementsByTagName('listname')[0].childNodes[0].data
return roms, compagny
roms, compagny = databaseGameExtraction('Nintendo Entertainment System.xml')
print(compagny)
print(roms)
</code></pre>