Re: [web2py] xml.etree
There is nothing wrong with your code. Maybe it is better to use find() to get the geo tag. If you have an XSD schema you can use generateDS http://www.rexx.com/~dkuhlman/generateDS.html to have a python parser. But if you can use the JSON API... much simpler. mic 2011/9/19 Chris Rowson christopherrow...@gmail.com: Anybody using xml.etree? I asked this question over at the Python tutors group but it seems that few people there had experience of it. I'm trying to access a UK postcode API at www.uk-postcodes.com to take a UK postcode and return the lat/lng of the postcode. This is what the XML looks like: http://www.uk-postcodes.com/postcode/HU11AA.xml The function below returns a dict with the xml tag as a key and the text as a value. Is this a correct way to use xml.etree? Is there a better way of doing this? Thanks in advance! Chris def ukpostcodesapi(postcode): import urllib import xml.etree.ElementTree as etree baseURL='http://www.uk-postcodes.com/' geocodeRequest='postcode/'+postcode+'.xml' #grab the xml tree=etree.parse(urllib.urlopen(baseURL+geocodeRequest)) root=tree.getroot() results={} for child in root[1]: #here's the geo tag results.update({child.tag:child.text}) #build a dict containing the geocode data return results #example usage (testing the function) results = ukpostcodesapi('hu11aa') print results['lat']+' '+results['lng']
Re: [web2py] xml.etree
Thank you Michele, Chris On Mon, Sep 19, 2011 at 2:00 PM, Michele Comitini michele.comit...@gmail.com wrote: There is nothing wrong with your code. Maybe it is better to use find() to get the geo tag. If you have an XSD schema you can use generateDS http://www.rexx.com/~dkuhlman/generateDS.html to have a python parser. But if you can use the JSON API... much simpler. mic 2011/9/19 Chris Rowson christopherrow...@gmail.com: Anybody using xml.etree? I asked this question over at the Python tutors group but it seems that few people there had experience of it. I'm trying to access a UK postcode API at www.uk-postcodes.com to take a UK postcode and return the lat/lng of the postcode. This is what the XML looks like: http://www.uk-postcodes.com/postcode/HU11AA.xml The function below returns a dict with the xml tag as a key and the text as a value. Is this a correct way to use xml.etree? Is there a better way of doing this? Thanks in advance! Chris def ukpostcodesapi(postcode): import urllib import xml.etree.ElementTree as etree baseURL='http://www.uk-postcodes.com/' geocodeRequest='postcode/'+postcode+'.xml' #grab the xml tree=etree.parse(urllib.urlopen(baseURL+geocodeRequest)) root=tree.getroot() results={} for child in root[1]: #here's the geo tag results.update({child.tag:child.text}) #build a dict containing the geocode data return results #example usage (testing the function) results = ukpostcodesapi('hu11aa') print results['lat']+' '+results['lng']
Re: [web2py] xml.etree
I use xml minidom and it is easier. http://zerp.ly/rochacbruno Em 19/09/2011 11:40, Chris Rowson christopherrow...@gmail.com escreveu: Thank you Michele, Chris On Mon, Sep 19, 2011 at 2:00 PM, Michele Comitini michele.comit...@gmail.com wrote: There is nothing wrong with your code. Maybe it is better to use find() to get the geo tag. If you have an XSD schema you can use generateDS http://www.rexx.com/~dkuhlman/generateDS.html to have a python parser. But if you can use the JSON API... much simpler. mic 2011/9/19 Chris Rowson christopherrow...@gmail.com: Anybody using xml.etree? I asked this question over at the Python tutors group but it seems that few people there had experience of it. I'm trying to access a UK postcode API at www.uk-postcodes.com to take a UK postcode and return the lat/lng of the postcode. This is what the XML looks like: http://www.uk-postcodes.com/postcode/HU11AA.xml The function below returns a dict with the xml tag as a key and the text as a value. Is this a correct way to use xml.etree? Is there a better way of doing this? Thanks in advance! Chris def ukpostcodesapi(postcode): import urllib import xml.etree.ElementTree as etree baseURL='http://www.uk-postcodes.com/' geocodeRequest='postcode/'+postcode+'.xml' #grab the xml tree=etree.parse(urllib.urlopen(baseURL+geocodeRequest)) root=tree.getroot() results={} for child in root[1]: #here's the geo tag results.update({child.tag:child.text}) #build a dict containing the geocode data return results #example usage (testing the function) results = ukpostcodesapi('hu11aa') print results['lat']+' '+results['lng']
Re: [web2py] xml.etree
I did look at that Bruno, but some of the articles I read suggested that it is not very memory efficient? Chris On Sep 19, 2011 3:44 PM, Bruno Rocha rochacbr...@gmail.com wrote: I use xml minidom and it is easier. http://zerp.ly/rochacbruno Em 19/09/2011 11:40, Chris Rowson christopherrow...@gmail.com escreveu: Thank you Michele, Chris On Mon, Sep 19, 2011 at 2:00 PM, Michele Comitini michele.comit...@gmail.com wrote: There is nothing wrong with your code. Maybe it is better to use find() to get the geo tag. If you have an XSD schema you can use generateDS http://www.rexx.com/~dkuhlman/generateDS.html to have a python parser. But if you can use the JSON API... much simpler. mic 2011/9/19 Chris Rowson christopherrow...@gmail.com: Anybody using xml.etree? I asked this question over at the Python tutors group but it seems that few people there had experience of it. I'm trying to access a UK postcode API at www.uk-postcodes.com to take a UK postcode and return the lat/lng of the postcode. This is what the XML looks like: http://www.uk-postcodes.com/postcode/HU11AA.xml The function below returns a dict with the xml tag as a key and the text as a value. Is this a correct way to use xml.etree? Is there a better way of doing this? Thanks in advance! Chris def ukpostcodesapi(postcode): import urllib import xml.etree.ElementTree as etree baseURL='http://www.uk-postcodes.com/' geocodeRequest='postcode/'+postcode+'.xml' #grab the xml tree=etree.parse(urllib.urlopen(baseURL+geocodeRequest)) root=tree.getroot() results={} for child in root[1]: #here's the geo tag results.update({child.tag:child.text}) #build a dict containing the geocode data return results #example usage (testing the function) results = ukpostcodesapi('hu11aa') print results['lat']+' '+results['lng']
Re: [web2py] xml.etree
I dont know your needs but, you can pass any XML to web2py TAG[''] helper so then you can use Server side DOM to inspect http://web2py.com/book/default/chapter/05#Server-side-DOM-and-Parsing
Re: [web2py] xml.etree
I like lxml more , and check pyquery! jQuery of python at server side. On 9/20/11, Bruno Rocha rochacbr...@gmail.com wrote: I dont know your needs but, you can pass any XML to web2py TAG[''] helper so then you can use Server side DOM to inspect http://web2py.com/book/default/chapter/05#Server-side-DOM-and-Parsing
Re: [web2py] xml.etree
Thanks all! Chris On Mon, Sep 19, 2011 at 6:49 PM, Phyo Arkar phyo.arkarl...@gmail.com wrote: I like lxml more , and check pyquery! jQuery of python at server side. On 9/20/11, Bruno Rocha rochacbr...@gmail.com wrote: I dont know your needs but, you can pass any XML to web2py TAG[''] helper so then you can use Server side DOM to inspect http://web2py.com/book/default/chapter/05#Server-side-DOM-and-Parsing