trouble with minidom

Miles Kaufmann milesck at umich.edu
Wed Jul 22 03:21:06 EDT 2009


On Jul 21, 2009, at 8:08 PM, Ronn Ross wrote:

> Hello I'm trying to read an xml file using minidome. The xml looks  
> like:
> <rootNode>
>    <project>
>       <name>myProj</name>
>       <path>/here/</path>
>    </project>
> </rootNode>
>
> My code looks like so:
> from xml.dom.minidom import parse
>
> dom = parse("myfile.xml")
>
> for node in dom.getElementsByTagName("project'):
>    print('name: %s, path: %s \n') % (node.childNodes[0].nodeValue,  
> node.childNodes[1])
>
> Unfortunately, it returns 'nodeValue as none. I'm trying to read the  
> value out of the node fir example name: myProj. I haven't found much  
> help in the documentation. Can someone point me in the right  
> direction?

Two issues:

In your example XML file, the first child node of the project Element  
is the Text node containing the whitespace between the <project> and  
<name> tags.  node.childNodes[0] will select that whitespace node.

The nodeValue of an Element is null (None).  In order to get the text  
contents of an element, you must get the nodeValue of the Text child  
node of the Element.

Like Gabriel, I would recommend using an XML library with a more  
concise API than the W3C DOM (I like lxml.objectify).  But if you  
stick with xml.dom, use the specification as a reference:

http://www.w3.org/TR/REC-DOM-Level-1/



More information about the Python-list mailing list