Parsing a URL XML with the ElementTree XML API

15,824

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....
Share:
15,824
fear_matrix
Author by

fear_matrix

I have around 7 years of experience and currently working for crondeals.com which is a deals and coupons site. I main expertise is in Python but lately I have developed interested in UI and SEO.

Updated on June 04, 2022

Comments

  • fear_matrix
    fear_matrix about 2 years

    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']