I am trying to read in a simple xml file and display all the child elements all i get is the error

Traceback (most recent call last):
File "/Users/adamplowman/Documents/Uni/project/python/test_xml.py", line 10, in <module>
for element in tree:
TypeError: iteration over non-sequence

Here is the xml file

<root><child>One</child><child>Two</child></root>

and this is the python file

#!/usr/bin/env python

from xml.etree import ElementTree as ET

try:
    tree = ET.parse('/Users/adamplowman/Desktop/sample.xml')
except Exception, inst:
    print "Unexpected error opening"
 
for subelement in tree:
    print subelement.text

if i do this within the python file

#!/usr/bin/env python

from xml.etree import ElementTree as ET

def main():
	element = ET.XML("<root><child>One</child><child>Two</child></root>")
	for subelement in element:
		print subelement.text

if __name__ == "__main__":
	# Someone is launching this directly
	main()

everything works fine. I am using this website for help http://www.learningpython.com/2008/05/07/elegant-xml-parsing-using-the-elementtree-module/#ReadingfromtheWeb

any help is appreciated

i have solved the problem with this script

#!/usr/bin/env python

from xml.etree import ElementTree as ET
import os
import urllib


feed = urllib.urlopen("http://server-up.theatticnetwork.net/demo/")
try:
    tree = ET.parse(feed)
		
except Exception, inst:
    print "Unexpected error opening %s: %s" % (tree, inst)
    
root= tree.getroot()

for subelement in root:
    if subelement.text is None:
       for subelement in subelement:
           if subelement.text is None:
               for subelement in subelement:
                   if subelement.text is None:
                       for subelement in subelement:
                           print subelement.text
                   else:
                        print subelement.text
           else:
               print subelement.text
    else: print subelement.text

can anyone see how i could improve the looping

for subelement in root:
    if subelement.text is None:
       for subelement in subelement:
           if subelement.text is None:
               for subelement in subelement:
                   if subelement.text is None:
                       for subelement in subelement:
                           print subelement.text
                   else:
                        print subelement.text
           else:
               print subelement.text
    else: print subelement.text

I suggest a generator

def find_text(element):
    if element.text is None:
        for subelement in element:
            for txt in find_text(subelement):
                yield txt
    else:
        yield element.text

for txt in find_text(root):
    print txt

That works beautifully and i understand it. How would i go about inserting the error name as a key within a dictionary and having the value as element.text

ok i have sorted out the dictionary stage. Now to build on the coding. I have the xml file like

<Network>
<NetDevice>
<Name>lo</Name>
<RxBytes>2596219</RxBytes>
<TxBytes>2596219</TxBytes>
<Errors>0</Errors>
<Drops>0</Drops>
</NetDevice>
<NetDevice>
<Name>eth1</Name>
<RxBytes>0</RxBytes>
<TxBytes>0</TxBytes>
<Errors>0</Errors>
<Drops>0</Drops>
</NetDevice>
<NetDevice>
<Name>eth0</Name>
<RxBytes>292008101</RxBytes>
<TxBytes>3198114577</TxBytes>
<Errors>0</Errors>
<Drops>0</Drops>
</NetDevice>

i want to pull out the indivdual section of information, Netdevice, and store it in its own dictionary.

the full xml is stored http://server-up.theatticnetwork.net/demo/

any clues are appreciated

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.