我的 XML 结构如下所示,但规模更大:
<root>
<conference name='1'>
<author>
Bob
</author>
<author>
Nigel
</author>
</conference>
<conference name='2'>
<author>
Alice
</author>
<author>
Mary
</author>
</conference>
</root>
为此,我使用了以下代码:
dom = parse(filepath)
conference=dom.getElementsByTagName('conference')
for node in conference:
conf_name=node.getAttribute('name')
print conf_name
alist=node.getElementsByTagName('author')
for a in alist:
authortext= a.nodeValue
print authortext
但是,打印出来的作者文本是“无”。我尝试使用如下所示的变体来搞乱,但这会导致我的程序中断。
authortext=a[0].nodeValue
正确的输出应该是:
1
Bob
Nigel
2
Alice
Mary
但我得到的是:
1
None
None
2
None
None
关于如何解决这个问题的任何建议?