Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing a URL XML with the ElementTree XML API

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']
like image 487
fear_matrix Avatar asked Feb 14 '23 02:02

fear_matrix


1 Answers

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....
like image 61
arocks Avatar answered Feb 22 '23 20:02

arocks