[Tutor] How to get a string from a DOM Text node ?

Martin Walsh mwalsh at groktech.org
Mon May 26 02:04:23 CEST 2008


Zameer Manji wrote:
> I'm trying to extract some information from the following xml file:
> http://pastebin.ca/1029125
> 

> file = "Music.xml"

> dict = plist.childNodes[1] #dict contains App version and stuff, as well

You really should try to avoid rebinding built-in names, like dict and
file, this is just asking for trouble.

> The problem is that the Text node that is printed out is blank, when it
> should be 42. What do I need to do so I can get tdict.childNodes[2] to
> become a string being "42" ?

I have not used minidom at all really, but here is an example using
BeautifulSoup, perhaps you'll find it helpful:

from BeautifulSoup import BeautifulStoneSoup

class MusicSoup(BeautifulStoneSoup):
    NESTABLE_TAGS = {
        'dict': [], 'array': []
    }


bsoup = MusicSoup(file('Music.xml'))
print bsoup.dict.dict.key.string
# 42
print bsoup.dict.dict.key.findNextSibling('key').string
# 183

... or ...

tracks = bsoup.dict.dict.findChildren('key', recursive=False)
print tracks

for track in tracks:
    print track
    details = track.findNextSibling('dict')


HTH,
Marty




More information about the Tutor mailing list