Reading XML data
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
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
adam291086
Junior Poster in Training
61 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
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
adam291086
Junior Poster in Training
61 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
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
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
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
adam291086
Junior Poster in Training
61 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
ok i have sorted out the dictionary stage. Now to build on the coding. I have the xml file like
lo2596219259621900eth10000eth0292008101319811457700
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
adam291086
Junior Poster in Training
61 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0