Reading XML data

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2008
Posts: 58
Reputation: adam291086 is an unknown quantity at this point 
Solved Threads: 0
adam291086 adam291086 is offline Offline
Junior Poster in Training

Reading XML data

 
0
  #1
Nov 24th, 2008
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

  1. <root><child>One</child><child>Two</child></root>

and this is the python file

  1. #!/usr/bin/env python
  2.  
  3. from xml.etree import ElementTree as ET
  4.  
  5. try:
  6. tree = ET.parse('/Users/adamplowman/Desktop/sample.xml')
  7. except Exception, inst:
  8. print "Unexpected error opening"
  9.  
  10. for subelement in tree:
  11. print subelement.text

if i do this within the python file

  1. #!/usr/bin/env python
  2.  
  3. from xml.etree import ElementTree as ET
  4.  
  5. def main():
  6. element = ET.XML("<root><child>One</child><child>Two</child></root>")
  7. for subelement in element:
  8. print subelement.text
  9.  
  10. if __name__ == "__main__":
  11. # Someone is launching this directly
  12. main()

everything works fine. I am using this website for help http://www.learningpython.com/2008/0...dingfromtheWeb

any help is appreciated
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 58
Reputation: adam291086 is an unknown quantity at this point 
Solved Threads: 0
adam291086 adam291086 is offline Offline
Junior Poster in Training

Re: Reading XML data

 
0
  #2
Nov 24th, 2008
i have solved the problem with this script
  1. #!/usr/bin/env python
  2.  
  3. from xml.etree import ElementTree as ET
  4. import os
  5. import urllib
  6.  
  7.  
  8. feed = urllib.urlopen("http://server-up.theatticnetwork.net/demo/")
  9. try:
  10. tree = ET.parse(feed)
  11.  
  12. except Exception, inst:
  13. print "Unexpected error opening %s: %s" % (tree, inst)
  14.  
  15. root= tree.getroot()
  16.  
  17. for subelement in root:
  18. if subelement.text is None:
  19. for subelement in subelement:
  20. if subelement.text is None:
  21. for subelement in subelement:
  22. if subelement.text is None:
  23. for subelement in subelement:
  24. print subelement.text
  25. else:
  26. print subelement.text
  27. else:
  28. print subelement.text
  29. else: print subelement.text

can anyone see how i could improve the looping

  1. for subelement in root:
  2. if subelement.text is None:
  3. for subelement in subelement:
  4. if subelement.text is None:
  5. for subelement in subelement:
  6. if subelement.text is None:
  7. for subelement in subelement:
  8. print subelement.text
  9. else:
  10. print subelement.text
  11. else:
  12. print subelement.text
  13. else: print subelement.text
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 984
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 222
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: Reading XML data

 
0
  #3
Nov 24th, 2008
I suggest a generator
  1. def find_text(element):
  2. if element.text is None:
  3. for subelement in element:
  4. for txt in find_text(subelement):
  5. yield txt
  6. else:
  7. yield element.text
  8.  
  9. for txt in find_text(root):
  10. print txt
Last edited by Gribouillis; Nov 24th, 2008 at 11:25 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 58
Reputation: adam291086 is an unknown quantity at this point 
Solved Threads: 0
adam291086 adam291086 is offline Offline
Junior Poster in Training

Re: Reading XML data

 
0
  #4
Nov 24th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 58
Reputation: adam291086 is an unknown quantity at this point 
Solved Threads: 0
adam291086 adam291086 is offline Offline
Junior Poster in Training

Re: Reading XML data

 
0
  #5
Nov 25th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum


Views: 889 | Replies: 4
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC