[Tutor] encoding question

Alex Kleider akleider at sonic.net
Sun Jan 5 07:32:27 CET 2014


On 2014-01-04 21:20, Danny Yoo wrote:
> Oh!  That's unfortunate!  That looks like a bug on the hostip.info
> side.  Check with them about it.
> 
> 
> I can't get the source code to whatever is implementing the JSON
> response, so I can not say why the city is not being properly included
> there.
> 
> 
> [... XML rant about to start.  I am not disinterested, so my apologies
> in advance.]
> 
> ... In that case... I suppose trying the XML output is a possible
> approach.  But I truly dislike XML for being implemented in ways that
> are usually not fun to navigate: either the APIs or the encoded data
> are usually convoluted enough to make it a chore rather than a
> pleasure.
> 
> The beginning does look similar:
> 
> ##############################################################
>>>> import xml.etree.ElementTree as ET
>>>> import urllib
>>>> response = 
>>>> urllib.urlopen("http://api.hostip.info?ip=201.234.178.62&position=true")
>>>> tree = ET.parse(response)
>>>> tree
> <xml.etree.ElementTree.ElementTree object at 0x185a2d0>
> ##############################################################
> 
> 
> Up to this point, not so bad.  But this is where it starts to look 
> silly:
> 
> ##############################################################
>>>> tree.find('{http://www.opengis.net/gml}featureMember/Hostip/ip').text
> '201.234.178.62'
>>>> tree.find('{http://www.opengis.net/gml}featureMember/Hostip/{http://www.opengis.net/gml}name').text
> u'Bogot\xe1'
> ##############################################################
> 
> where we need to deal with XML namespaces, an extra complexity for a
> benefit that I have never bought into.
> 
> 
> More than that, usually the XML I run into in practice isn't even
> properly structured, as is the case with the lat-long value in the XML
> output here:
> 
> ##############################################################
>>>> tree.find('.//{http://www.opengis.net/gml}coordinates').text
> '-75.2833,10.4'
> ##############################################################
> 
> which is truly silly.  Why is the latitude and longitude not two
> separate, structured values?  What is this XML buying us here, really
> then?  I'm convinced that all the extraneous structure and complexity
> in XML causes the people who work with it to stop caring, the result
> being something that isn't for the benefit of either humans nor
> computer programs.
> 
> 
> Hence, that's why I prefer JSON: JSON export is usually a lot more
> sensible, for reasons that I can speculate on, but I probably should
> stop this rant.  :P

Not a rant at all.

As it turns out, one of the other things that have interested me of late 
is docbook, an xml dialect (I think this is the correct way to express 
it.)  I've found it very useful and so do not share your distaste for 
xml although one can't disagree with the points you've made with regard 
to xml as a solution to the problem under discussion.
I've not played with the python xml interfaces before so this will be a 
good project for me.

Thanks.


More information about the Tutor mailing list