使用Python minidom读取XML并遍历每个节点 [英] Reading XML using Python minidom and iterating over each node
本文介绍了使用Python minidom读取XML并遍历每个节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个如下所示的XML结构,但是规模更大:
I have an XML structure that looks like the following, but on a much larger scale:
<root>
<conference name='1'>
<author>
Bob
</author>
<author>
Nigel
</author>
</conference>
<conference name='2'>
<author>
Alice
</author>
<author>
Mary
</author>
</conference>
</root>
为此,我使用了以下代码:
For this, I used the following code:
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
但是,打印出来的作者文本为无".我尝试使用下面的变体来弄乱自己,但是这会导致我的程序中断.
However, the authortext that is printed out is 'None.' I tried messing around with using variations like what is below, but it causes my program to break.
authortext=a[0].nodeValue
正确的输出应为:
1
Bob
Nigel
2
Alice
Mary
但是我得到的是:
1
None
None
2
None
None
关于如何解决此问题的任何建议?
Any suggestions on how to tackle this problem?
推荐答案
您的authortext
类型为1(ELEMENT_NODE
),通常需要TEXT_NODE
才能获取字符串.这将起作用
your authortext
is of type 1 (ELEMENT_NODE
), normally you need to have TEXT_NODE
to get a string. This will work
a.childNodes[0].nodeValue
这篇关于使用Python minidom读取XML并遍历每个节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文