[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: DOM parsing not working!

rbossy

3/8/2008 2:09:00 PM

Quoting Mike D <42flicks@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