[Tutor] XML: Expletive Deleted

lawrence wang levity at gmail.com
Sat Jun 10 08:31:55 CEST 2006

>  >> for item in itemIDs:
>  >>     print item
>  yeilds
>  <DOM Element: ItemID at 0x7f532c0c>
>  <DOM Element: ItemID at 0x7f5400cc>
>  <DOM Element: ItemID at 0x7f54656c>
>  <DOM Element: ItemID at 0x7f54ea0c>
>  <DOM Element: ItemID at 0x7f555eac>
>  Okay, no problem. Now all I have to do is figure out which
> particlular.string.of.words.interconnected.by.periods to
> pass to extract the values.
> >> for item in itemIDs:
> >>     print item.nodeValue
> Seems logical:
> None
> None
> None
> None
> None

try dir(item) to see what attributes the item has, and try the ones
that sound right. e.g.:

>>> from xml.dom.minidom import parse, parseString
>>> resp = parseString("<top><middle><bottom>foo</bottom></middle></top>")
>>> bottom = resp.getElementsByTagName("bottom")
>>> bottom
[<DOM Element: bottom at 0x2aaaadc26c68>]
>>> dir(bottom[0])
['ATTRIBUTE_NODE', ...long list snipped..., 'writexml']
>>> bottom[0].hasChildNodes()
>>> bottom[0].childNodes
[<DOM Text node "foo">]
>>> dir(bottom[0].childNodes[0])
['ATTRIBUTE_NODE', ...long list snipped..., 'writexml']
>>> bottom[0].childNodes[0].data

so you see, with "<tag>value</tag>", there's an invisible text node.
it's one of the quirks of xml, i guess. then the attribute you're
looking for is "data", not "nodeValue".

in summary: instead of item.nodeValue, item.childNodes[0].data.


More information about the Tutor mailing list