4

Below is my sample code where in the background I am downloading statsxml.jsp with wget and then parsing the xml. My question is now I need to parse multiple XML URL and as you can see in the below code I am using one single file. How to accomplish this?

Example URL - http://www.trion1.com:6060/stat.xml, http://www.trion2.com:6060/stat.xml, http://www.trion3.com:6060/stat.xml

import xml.etree.cElementTree as ET
tree = ET.ElementTree(file='statsxml.jsp')

root = tree.getroot()
root.tag, root.attrib

print "root subelements: ", root.getchildren()
root.getchildren()[0][1]
root.getchildren()[0][4].getchildren()

for component in tree.iterfind('Component name'):
    print component.attrib['name']
1
  • Perhaps I've understood incorrectly, but can't you simply have an array of these "multiple urls" and call this snippet (probably in a function or simply in a loop) "for each url in array_of_urls" ? Jan 17, 2014 at 6:42

1 Answer 1

8

You can use urllib2 to download and parse the file in the same way. For e.g. the first few lines will be changed to:

import xml.etree.cElementTree as ET
import urllib2

for i in range(3):
    tree = ET.ElementTree(file=urllib2.urlopen('http://www.trion%i.com:6060/stat.xml' % i ))


    root = tree.getroot()
    root.tag, root.attrib

    # Rest of your code goes here....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.