| | |
Reading XML data
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2008
Posts: 58
Reputation:
Solved Threads: 0
I am trying to read in a simple xml file and display all the child elements all i get is the error
Here is the xml file
and this is the python file
if i do this within the python file
everything works fine. I am using this website for help http://www.learningpython.com/2008/0...dingfromtheWeb
any help is appreciated
•
•
•
•
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
Python Syntax (Toggle Plain Text)
<root><child>One</child><child>Two</child></root>
and this is the python file
Python Syntax (Toggle Plain Text)
#!/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
Python Syntax (Toggle Plain Text)
#!/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/0...dingfromtheWeb
any help is appreciated
•
•
Join Date: Nov 2008
Posts: 58
Reputation:
Solved Threads: 0
i have solved the problem with this script
can anyone see how i could improve the looping
Python Syntax (Toggle Plain Text)
#!/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
Python Syntax (Toggle Plain Text)
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
python Syntax (Toggle Plain Text)
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
Last edited by Gribouillis; Nov 24th, 2008 at 11:25 am.
•
•
Join Date: Nov 2008
Posts: 58
Reputation:
Solved Threads: 0
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
<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
![]() |
Similar Threads
- Data feed url linking (PHP)
- Display xml data in message box (VB.NET)
- Reading a xml file from a web application. (Java)
- What exactly is xml? (RSS, Web Services and SOAP)
- How to re-organized XML file and call XML file from my software (RSS, Web Services and SOAP)
- Error reading XML file in Javascript (JavaScript / DHTML / AJAX)
- Parse XML from ASP!!! (ASP)
- Fake Microsoft Windows Security Warning (Viruses, Spyware and other Nasties)
- problem reading xml file in c# (C#)
Other Threads in the Python Forum
- Previous Thread: Adding info to a variable
- Next Thread: Python Counterpart?
Views: 889 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for Python
address anydbm app bash beginner bits calling class code conversion coordinates copy dan08 dictionary directory dynamic edit examples excel feet file float format ftp function generator gui halp homework http i/o images import info input ip itunes java keycontrol line linux list lists loop maintain millimeter mouse newb number numbers output panel parsing path port prime print program programming projects py-mailer py2exe pygame pyqt python queue random rational recursion recursive scrolledtext server smtp split ssh statictext string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial type ubuntu unicode update urllib urllib2 variable whileloop windows write wxpython





