DOM parsing not working!

rbossy at jouy.inra.fr rbossy at jouy.inra.fr
Sat Mar 8 15:09:20 CET 2008


Quoting Mike D <42flicks at gmail.com>:

> Hello, I've spent the morning trying to parse a simple xml file and have the
> following:
> import sys
> from xml.dom import minidom
>
> doc=minidom.parse('topstories.xml')
>
> items = doc.getElementsByTagName("item")
> text=''
> for i in items:
>     t = i.firstChild
>     print t.nodeName
>     if t.nodeType == t.TEXT_NODE:
>         print "TEXT_NODE"
>         print t.nodeValue
>         text += t.data
>
> print text
>
> I can't figure out how to print the text value for a text node type. There
> must be something obvious I'm missing, any suggestions?

Yes quite a trivial thing. t is assigned to the first child node of <item>
which, in your example, is a text node containg just a newline.
It will be shown if you replace your print statements with something like:

    print 't.nodeValue:', t.nodeValue, '### end of t.nodeValue'
    ...
print 'text:', text, '### end of text'


What is that you're trying to do? Do you want to extract all text nodes inside
<item>?


RB



More information about the Python-list mailing list