Reading XML using Python minidom and iterating over each node

GobiasKoffi picture GobiasKoffi · Sep 11, 2009 · Viewed 57.4k times · Source

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

The correct output should be:

1
Bob
Nigel
2
Alice
Mary

But what I get is:

1
None
None
2
None
None

Any suggestions on how to tackle this problem?

Answer

SilentGhost picture SilentGhost · Sep 11, 2009

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