Get XML file from the net and parse it
By Damien LEFEVRE, Tuesday 13 March 2007 at 20:27 :: Code Snippets :: #44 :: rss
In that piece of source I demonstrate how I get info on a server via XML file. You just need to write the XML parser to extract an condition the data as you need.
For parsing XML documents, I'm using cElementTree ported on PyS60 by Simo Salminen (available 2nd & 3rd edition).
You need to write a server script that generate your XML document if it is not static. PHP, JSP, Python... Up to you :)
import httplib class StockEngine( object ): def __init__( self, aHost, aPort=80 ): self._iHost = aHost self._iPort = aPort # creates connection with the server self._iConn = httplib.HTTPConnection( self._iHost, self._iPort ) def get( self ): # tags are sent in the header headers = { "Content-type" : "multipart/form-data", "Accept" : "text/plain", # you can hide in the header some authentication info or relevant # stuff "cipher" : "blablablablablabla" } params = None # send a request self._iConn.request("POST", "/yourPage/stock.jsp", # could also be php or # others... params, # here you can put some # parameter to get only one # category of product for # example headers) # get the response to be parsed from the server response = self._iConn.getresponse() # status == 200 means that the request was successfully received if response.status == 200: # parse login to extrat statement and UID try: return self._parseStock( response.read( ) ) except: return False else: return False def _parseStock( self, aSting ): stock = None # parse document from string # write your own parser here to store in stock return stock def __del__( self ): # make sure that the connection is closed if self._iConn: self._iConn.close( ) if __name__ == "__main__": stockEngine = StockEngine( "yourHostServer.XXX" ) stock = stockEngine.get() print stock