En Fri, 24 Jul 2009 14:56:29 -0300, Ronn Ross <ronn.r...@gmail.com> escribió:
On Tue, Jul 21, 2009 at 7:32 PM, Gabriel Genellina
<gagsl-...@yahoo.com.ar>wrote:
En Tue, 21 Jul 2009 21:08:57 -0300, Ronn Ross <ronn.r...@gmail.com>
escribió:

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>

I have used the loop below and it works great, but I need get both child
elements or 'project' per iteration. I want to build a dictionary that
resemble this:
my_dict = {'myProj':'/here/', 'anothername':'anotherpath'}

I couldn't find how to do with in the element tree docs. Can you point me in
the right direction?

py> import xml.etree.ElementTree as ET
py> xml = """<rootNode>
... <project>
...   <name>myProj</name>
...   <path>/here/</path>
... </project>
... <project>
...   <name>anothername</name>
...   <path>anotherpath</path>
... </project>
... </rootNode>"""
py> doc = ET.fromstring(xml)
py> result = {}
py> for project in doc.findall('project'):
...   name = project.find("name").text
...   path = project.find("path").text
...   result[name] = path
...
py> result
{'anothername': 'anotherpath', 'myProj': '/here/'}

The Element interface is documented here:
http://docs.python.org/library/xml.etree.elementtree.html#the-element-interface
More info in the author's site:
http://www.effbot.org/zone/element-index.htm

(Perhaps you're using Python 2.5? That part of the documentation was omited in the 2.5 doc set by mistake. Read the 2.6 docs instead, there was no changes)

--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to