Cherry Py - Return output as XML in Python -


My intention is to implement a web service in Google App Engine. I am using CherryPy because it is very easy to understand.

  import sys.path.insert (0, 'cherrypy.zip') Import Cherrypy Import Expose Class Converter from CheroePop: @expose Def Index (self): Returns "Hello World!" @expose def fahr_to_celc (self, degree): temp = (float (degree) - 32) * 5/9 return "% .01f"% temp @expose def celc_to_fahr (self, degree): temp = float (degree) * 9   

I would like to know how to return output in XML format, such as

  & lt ;? XML version = "1.0" encoding = "UTF-8"? & Gt; & Lt; Root & gt; & Lt; Answer & gt; Hello World! & Lt; / Answer & gt; & Lt; / Root & gt;   

I'm starting in Python, please help me.

Hariharan

I had a similar problem. My solution was to use xml elementtree, it was something like

  .... #elementtree is stored in weird places ... it mostly tries to: import xml.etree As ElementTree in # # python & gt; Except = 2.5 ImportError: Try: Import #Efbot C module, except importError as imported cElementTree: Try: ImportError Exclude ET # importbutton as the pure Python module of importtree.ElementTree: Try: et # ElementTree Import lxml.etree as API, by using libxml2: import warnings warnings.warn ("Could not import ElementTree" "(http://effbot.org/zone/element-index.htm)") def build_xml_tree (Answer_txt = ""): (resource) does not lane, then: Return "" root = ET.Element ("root") Answer = ET.SubElement T, "the answer") answer.text = answer_txt xml_string = ET.tostring (root) return rxml_string   

Then call build_xml_tree

Comments