Easy question on minidom

Alex Martelli aleax at mac.com
Sun Jun 18 07:22:42 CEST 2006

Dean Card <mailer at forums.com> wrote:

> I am using minidom to parse a 20,000 line XML file.  I have a few instances
> where the number of child nodes of a particular node can be variable in
> number.  To access them I am doing something like the following...
> xmldoc = minidom.parseString(r)
> results = xmldoc.childNodes[0]
> for myNode in results.childNodes[1].childNodes:
>     do Stuff with myNode...
> problem is I am having a heck of a time trying to access the value of the
> node.  For instance I get things like this
> <DOM Element: PersonName at 0x8391dac>
> <DOM Element: PostalAddress at 0x8391f0c>
> <DOM Element: VoiceNumber at 0x842a18c>
> <DOM Element: VoiceNumber at 0x842a32c>
> <DOM Element: E-mail at 0x842a4cc>
> But I need the values... any help here? 

Not sure what you mean by "the values" -- if you mean for example the
contents of text data lying under the node, you can access them...:

>>> from xml.dom import minidom
>>> s='<foo><bar>uno</bar><baz>dos</baz></foo>'
>>> xmldoc = minidom.parseString(s)
>>> xmldoc.childNodes[0]
<DOM Element: foo at 0x81238>
>>> xmldoc.childNodes[0].childNodes
[<DOM Element: bar at 0x812d8>, <DOM Element: baz at 0x818f0>]
>>> for node in xmldoc.childNodes[0].childNodes: print node
<DOM Element: bar at 0x812d8>
<DOM Element: baz at 0x818f0>
>>> for node in xmldoc.childNodes[0].childNodes: print
<DOM Text node "uno">
<DOM Text node "dos">
>>> for node in xmldoc.childNodes[0].childNodes: print

i.e., the text node is the .childNodes[0] (or more elegantly the
.firstChild, of course) of the element node containing it, and if you
need that text node's text data, that's its .data attribute.

If you need something else, I suggest you post a small example (like
mine here) with a clear explanation of what exactly it is that you need!


More information about the Python-list mailing list