Easy question on minidom

D

Dean Card

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?
 
A

Alex Martelli

Dean Card said:
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]
xmldoc.childNodes[0].childNodes
for node in xmldoc.childNodes[0].childNodes: print node
....
for node in xmldoc.childNodes[0].childNodes: print
node.childNodes[0]
....
for node in xmldoc.childNodes[0].childNodes: print
node.childNodes[0].data
....
uno
dos
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!


Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,248
Latest member
MagdalenaB

Latest Threads

Top